硒动态元素Python [英] Selenium Dynamic Element Python

查看:74
本文介绍了硒动态元素Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用硒.我必须将密钥发送到此输入.

Hello I am using 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">

例如,每次刷新,输入ID都在变化.我如何用硒找到这个元素

For example every refresh, input id is changing. How can I find this element with selenium

推荐答案

您可以通过xpath找到它们

you can find them by xpath

即:

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

您可以通过:

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

此处的数字1表示其第一种形式. 在您的情况下,如果您知道表格,则可以使用以下内容(只需更改数字以匹配您的数字即可,即,如果它的第4个输入,则将值更改为4)

the number 1 here indicates that its the first form. in your case if you know the form you can use the following(just change the number to match yours. i.e if its the 4th input then change the value to 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']")

要验证xpath是否可以工作,请尝试使用Web检查器中的搜索框,它接受xpath,并且如果找到了您的元素,那么它也可以在python中工作.

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 了解更多方法.

这篇关于硒动态元素Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆