簡體   English   中英

Selenium 動態元件 Python

[英]Selenium Dynamic Element Python

您好,我正在使用 selenium。 我必須將密鑰發送到此輸入。

<input id="209f0c3d-3222-4caa-b55d-1d4463322fd4" type="email" placeholder="E-posta adresi" value="" name="emailAddress" data-componentname="emailAddress" autocomplete="email" autocorrect="off" autocapitalize="off" spellcheck="false">

<input id="8ccf12d3-e264-43b8-8bbe-70e1f3eef202" type="email" placeholder="E-posta adresi" value="" name="emailAddress" data-componentname="emailAddress" autocomplete="email" autocorrect="off" autocapitalize="off" spellcheck="false">

例如每次刷新,輸入 id 都會發生變化。 如何使用 selenium 找到這個元素

您可以通過 xpath 找到它們

IE:

<html>
 <body>
  <form id="loginForm">
</body>
<html>

你可以通過:

login_form = driver.find_element_by_xpath("/html/body/form[1]")

這里的數字 1 表示它是第一種形式。 在您的情況下,如果您知道可以使用以下形式的表格(只需更改數字以匹配您的數字。即,如果它是第 4 個輸入,則將值更改為 4)

driver.find_element_by_xpath("//form[1]/input[1]")

另一種選擇是在名稱、類型和其他一些屬性不變的情況下,您可以使用(鏈接它們以使其指向唯一元素):

driver.find_element_by_xpath("//input[@name='emailAddress'][@type='email']")

to validate if the xpath will work, try the search box in the web inspector, it accept xpath and if it finds your element, then it will work in python too.

更多方法請參考https://selenium-python.readthedocs.io/locating-elements.html

您可以使用 xpath kr css 定位元素,其中 id 或類名不是唯一的。

driver.find_element_by_xpath("//input[@name='emailAddress']")

或者

driver.find_element_by_name('emailAddress')

或者

driver.find_element_by_css_selector("input[name='emailAddress']")

注意:如果屬性組合是唯一的,您也可以進行鏈接:

driver.find_element_by_xpath("//input[@name='emailAddress'][@type='email']")

您可以為輸入字段使用任何唯一選擇器: type="email" placeholder="E-posta adresi" value="" name="emailAddress" data-componentname="emailAddress"

xpath:

driver.find_element_by_xpath("//input[@name='emailAddress' and contains(@placeholder, 'E-posta adresi']")

css:

driver.find_element_by_css_selector("input[name='emailAddress'][type='email']")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM