繁体   English   中英

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

[英]Choose element by partial ID using Selenium with 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#。

在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>

我可以通过获取元素列表

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

谢谢!

虽然答案是在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)')]')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM