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