使用Selenium和python按部分ID选择元素?

时间:2013-04-06 00:15:01

标签: python selenium getelementbyid

我正在尝试在Python 2.7中使用Selenium中的Web元素。元素ID如下所示:

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

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

我试图通过类似的方式达到它。

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的情况下搜索并获取此元素吗?

我发现下面回答了类似的问题,但它是在C#中。不幸的是,我不知道C#。

Finding an element by partial id with Selenium in 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)')]")

谢谢!

1 个答案:

答案 0 :(得分:6)

虽然答案在C#中,但通过使用CSS选择器,底层解决方案仍然是相同的:

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

或XPath也可以这样做:

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