简体   繁体   English

Selenium 动态元件 Python

[英]Selenium Dynamic Element Python

Hello I am using selenium.您好,我正在使用 selenium。 I have to send key to this input.我必须将密钥发送到此输入。

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

For example every refresh, input id is changing.例如每次刷新,输入 id 都会发生变化。 How can I find this element with selenium如何使用 selenium 找到这个元素

you can find them by xpath您可以通过 xpath 找到它们

ie: IE:

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

you can get by:你可以通过:

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

the number 1 here indicates that its the first form.这里的数字 1 表示它是第一种形式。 in your case if you know the form you can use the following(just change the number to match yours. ie if its the 4th input then change the value to 4)在您的情况下,如果您知道可以使用以下形式的表格(只需更改数字以匹配您的数字。即,如果它是第 4 个输入,则将值更改为 4)

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

also another alternative is in cases where name, type and some other attributes don't change you can use(chaining them so they point to a unique element):另一种选择是在名称、类型和其他一些属性不变的情况下,您可以使用(链接它们以使其指向唯一元素):

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

refer to https://selenium-python.readthedocs.io/locating-elements.html for more ways.更多方法请参考https://selenium-python.readthedocs.io/locating-elements.html

You Can Locate element using xpath kr css where id or classname is not unique.您可以使用 xpath kr css 定位元素,其中 id 或类名不是唯一的。

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

Or或者

driver.find_element_by_name('emailAddress')

Or或者

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

Note: you can do chaining aswell if combination of attributes are unique:注意:如果属性组合是唯一的,您也可以进行链接:

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

You use any of the unique selectors for the input field: type="email" placeholder="E-posta adresi" value="" name="emailAddress" data-componentname="emailAddress"您可以为输入字段使用任何唯一选择器: type="email" placeholder="E-posta adresi" value="" name="emailAddress" data-componentname="emailAddress"

xpath: xpath:

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

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