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

2024-04-26 12:03:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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)')]")

谢谢你!


Tags: id元素inputbydriverseleniumlinecell
1条回答
网友
1楼 · 发布于 2024-04-26 12:03:39

尽管答案是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)')]')

相关问题 更多 >