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

bin*_*ing 5 python selenium getelementbyid

我试图在Python 2.7中使用Selenium中的Web元素.元素ID如下所示:

cell.line.order(240686080).item(250444868).unitCost
Run Code Online (Sandbox Code Playgroud)

第一个数字字符串'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')
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,无论如何我们只能通过已知的部分ID来搜索和获取此元素吗?

我在下面找到了类似的问题,但它是在C#中.不幸的是,我不知道C#.

在C#中使用Selenium通过部分id查找元素

先感谢您!

编辑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>
Run Code Online (Sandbox Code Playgroud)

我可以通过获取元素列表

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

谢谢!

Arr*_*ran 9

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

driver.find_elements_by_css_selector('input[id*='cell.line.order(240686080)']')
Run Code Online (Sandbox Code Playgroud)

或者XPath也可以这样做:

driver.find_elements_by_xpath('//*[contains(@id, 'cell.line.order(240686080)')]')
Run Code Online (Sandbox Code Playgroud)