你好,我使用的是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找到这种元素?
发布于 2020-05-07 01:28:32
您可以通过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。
发布于 2020-05-07 03:09:55
您可以使用xpath kr css查找元素,其中id或classname不是唯一的。
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']")发布于 2020-05-07 04:31:30
您可以对输入字段使用任何唯一的选择器: 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']")https://stackoverflow.com/questions/61641202
复制相似问题