Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python使用随机id的Selenium表单输入_Python_Html_Selenium_Xpath - Fatal编程技术网

使用python使用随机id的Selenium表单输入

使用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

我正在尝试使用selenium和python登录到一个网站。这就是用户名和密码的html格式。名称和id每次都是随机字符串,那么如何使用selenium在表单中键入?我在chrome上使用inspect元素找到的xpath和css选择器都包含该id,但由于该id是随机的,因此实际上无法工作

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