使用python使用随机id的Selenium表单输入
我正在尝试使用selenium和python登录到一个网站。这就是用户名和密码的html格式。名称和id每次都是随机字符串,那么如何使用selenium在表单中键入?我在chrome上使用inspect元素找到的xpath和css选择器都包含该id,但由于该id是随机的,因此实际上无法工作使用python使用随机id的Selenium表单输入,python,html,selenium,xpath,Python,Html,Selenium,Xpath,我正在尝试使用selenium和python登录到一个网站。这就是用户名和密码的html格式。名称和id每次都是随机字符串,那么如何使用selenium在表单中键入?我在chrome上使用inspect元素找到的xpath和css选择器都包含该id,但由于该id是随机的,因此实际上无法工作 <input name="u41f98d000f26d904164eaf12622351bd" id="u41f98d000f26d904164eaf12622351bd" type="text" va
<input name="u41f98d000f26d904164eaf12622351bd" id="u41f98d000f26d904164eaf12622351bd" type="text" value="" autocomplete="off" tabindex="1">
<input name="pa0ef2cd4e22824cebe65dfed7f683c54" id="pa0ef2cd4e22824cebe65dfed7f683c54" type="password" value="" autocomplete="off" tabindex="2">
很抱歉,如果这是一个愚蠢的问题,我是python和selenium的新手,有两种可能会突出显示独特的选择器
type=“text”
和type=“password”
tabindex=“1”
和tabindex=“2”
tabindex
可能更好
user_inp = "username"
pass_inp = "password"
username = driver.find_element_by_css_selector("input[tabindex='1']")
password = driver.find_element_by_css_selector("input[tabindex='2']")
username.send_keys(user_inp)
password.send_keys(pass_inp)
如果要形成XPath,可以使用其中一个或两个,如下所述:
username = driver.find_element_by_xpath("//input[@tabindex='1' and @type='text']")
password = driver.find_element_by_xpath("//input[@tabindex='2' and @type='password']")