简体   繁体   English

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

[英]Choose element by partial ID using Selenium with python?

I am trying to reach an web element in Selenium in Python 2.7. 我试图在Python 2.7中使用Selenium中的Web元素。 The element ID is like this: 元素ID如下所示:

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

The first number string '240686080' is known, but the second number '250444868' is unknown beforehand. 第一个数字字符串'240686080'是已知的,但第二个数字'250444868'事先是未知的。

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')

So my questions is, is there anyway we can search and reach this element only with part of the ID known? 所以我的问题是,无论如何我们只能通过已知的部分ID来搜索和获取此元素吗?

I found similar question answered below, but it was in C#. 我在下面找到了类似的问题,但它是在C#中。 And unfortunately, I don't know C#. 不幸的是,我不知道C#。

Finding an element by partial id with Selenium in C# 在C#中使用Selenium通过部分id查找元素

Thank you in advance! 先感谢您!

Edited 4/8 编辑4/8

The element is coded like this: 元素编码如下:

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

I could get the element list by 我可以通过获取元素列表

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

Thank you! 谢谢!

Although the answer is in C#, the underlying solution is still the same, by using CSS selectors: 虽然答案是在C#中,但通过使用CSS选择器,底层解决方案仍然是相同的:

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

or XPath will also be able to do this: 或者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