![](/img/trans.png)
[英]How to Get the href attribute of the element using Selenium and Python
[英]How to get the for attribute of label element using selenium
我第一次嘗試使用 Python,並致力於 Selenium。
目標是獲取 Input 元素的 ID。 我正在嘗試使用為 Input 元素生成隨機 ID 的頁面。 因此無法通過 ID 來處理該元素。 我怎么發現該元素有一個 label,而 label 說 For="<Dynamic_ID_Of_Input>"
碰巧 label 也沒有其他屬性。
這是頁面的樣子
<div class="form-input">
<label for="labeled-input-Asrf3PAYKRKRY1veHroMKxyxf">First Name</label>
<input type="text" id="labeled-input-Asrf3PAYKRKRY1veHroMKxyxf" name="Asrf3PAYKRKRY1veHroMKxyxf" maxlength="79" class="" value="">
</div>
<div class="form-input">
<label for="labeled-input-7Hgp_pSJn3iqZ3T_eRwBX5I5n">Last Name</label
<input type="text" id="labeled-input-7Hgp_pSJn3iqZ3T_eRwBX5I5n" name="7Hgp_pSJn3iqZ3T_eRwBX5I5n" maxlength="79" class="" value="">
</div>
這是我到目前為止所管理的:
FName_ID = (driver.find_element_by_xpath("//label[contains(text(), 'First Name')]//ancestor::div//input").get_attribute("id"))
print("FName_ID",FName_ID)
LName_ID = (driver.find_element_by_xpath("//label[contains(text(), 'Last Name')]//ancestor::div//input").get_attribute("id"))
print("LName_ID",LName_ID)
打印 output 看起來像這樣:
FName_ID labeled-input-Asrf3PAYKRKRY1veHroMKxyxf
LName_ID labeled-input-Asrf3PAYKRKRY1veHroMKxyxf
我無法弄清楚這里缺少什么。 感謝所有幫助。
謝謝你
請試試這個:
FName_ID = (driver.find_element_by_xpath("//label[contains(text(), 'First Name')]/..//input").get_attribute("id"))
print("FName_ID",FName_ID)
LName_ID = (driver.find_element_by_xpath("//label[contains(text(), 'Last Name')]/..//input").get_attribute("id"))
print("LName_ID",LName_ID)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.