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