Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.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 查找Xpath对每个登录都是唯一的元素_Python_Html_Selenium_Xpath_Webautomation - Fatal编程技术网

Python 查找Xpath对每个登录都是唯一的元素

Python 查找Xpath对每个登录都是唯一的元素,python,html,selenium,xpath,webautomation,Python,Html,Selenium,Xpath,Webautomation,我正在编写一个python web自动化selenium脚本 在脚本中,我使用driver.find\u element\u by\u xpath'xpath'在Binary.com上查找元素。这意味着我必须预加载Binary.com并复制需要查找的元素的XPath。对于大多数元素,该方法都有效,但对于少数元素,我意识到Xpath对于每个登录都是唯一的 例如,如果我现在登录并尝试复制某个元素的xpath,它将是/*[@id=tp1602844250562],但如果重新加载页面或尝试登录到其他窗口

我正在编写一个python web自动化selenium脚本

在脚本中,我使用driver.find\u element\u by\u xpath'xpath'在Binary.com上查找元素。这意味着我必须预加载Binary.com并复制需要查找的元素的XPath。对于大多数元素,该方法都有效,但对于少数元素,我意识到Xpath对于每个登录都是唯一的

例如,如果我现在登录并尝试复制某个元素的xpath,它将是/*[@id=tp1602844250562],但如果重新加载页面或尝试登录到其他窗口,xpath将更改为/*[@id=tp1602844157070]。请注意,它们不是相同的id号。这意味着我不能在单独的页面登录上使用一个xpath

所需元素具有HTML代码:

为什么不使用类而不是id?请尝试以下xpath:


尝试将xpath表达式更改为

//input[starts-with(@id,"tp")]
或者,如果不总是输入


您可以在下面尝试使用class而不是id,因为我想id是从DB中提取出来的-

//div[@class='date-time']//input[contains(@class,'time')]

要使用该类查找输入元素,请使用

driver.find_element_by_css_selector("input.time.hasTimepicker")

这不是筛选类名的最佳方法。请看一下这个答案。
//div[@class='date-time']//input[contains(@class,'time')]
driver.find_element_by_css_selector("input.time.hasTimepicker")