使用Selenium和python按部分ID选择元素? [英] Choose element by partial ID using Selenium with python?

查看:154
本文介绍了使用Selenium和python按部分ID选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python 2.7中访问Selenium中的一个Web元素.元素ID如下:

I am trying to reach an web element in Selenium in Python 2.7. The element ID is like this:

cell.line.order(240686080).item(250444868).unitCost

第一个数字字符串'240686080'是已知的,但是第二个数字'250444868'是事先未知的.

The first number string '240686080' is known, but the second number '250444868' is unknown beforehand.

我试图通过这样的方式达到目标.

I tried to reach it by something like this.

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("somewebsite.com")
driver.find_elements_by_id('input.line.order(240417939).item('+ '\d{9}'+').unitCost')

所以我的问题是,我们是否只能通过已知ID的一部分来搜索并到达此元素?

So my questions is, is there anyway we can search and reach this element only with part of the ID known?

我发现下面回答了类似的问题,但这是在C#中.而且不幸的是,我不懂C#.

I found similar question answered below, but it was in C#. And unfortunately, I don't know C#.

使用部分ID查找元素C#中的硒

提前谢谢!

已编辑4/8

该元素的编码如下:

<td id="cell.line.order(240417939).item(250159165).unitCost" class="or_monetarydata">
    <input id="input.line.order(240417939).item(250159165).unitCost" type="hidden" value="135.00" 
    name="order(240417939).item(250159165).unitcost"></input>

    135.00

    </td>

我可以通过

value=driver.find_elements_by_xpath("//*[contains(@id, 'input.line.order(240417939)')]")

谢谢!

推荐答案

尽管答案在C#中,但是通过使用CSS选择器,基本解决方案仍然相同:

Although the answer is in C#, the underlying solution is still the same, by using CSS selectors:

driver.find_elements_by_css_selector('input[id*='cell.line.order(240686080)']')

或XPath也可以做到这一点:

or XPath will also be able to do this:

driver.find_elements_by_xpath('//*[contains(@id, 'cell.line.order(240686080)')]')

这篇关于使用Selenium和python按部分ID选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆