Selenium动态元素Python

2024-06-25 22:39:56 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我正在使用硒。我必须发送此输入的密钥

<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都在更改。如何找到含硒元素


Tags: nameidinputdatavalueemailtypeautocomplete
3条回答

您可以通过xpath找到它们

即:

<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']")

要验证xpath是否有效,请尝试web检查器中的搜索框,它接受xpath,如果它找到您的元素,那么它也可以在python中工作

有关更多方法,请参阅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']")

相关问题 更多 >