Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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 如何通过SeleniumWebDriver查找具有动态id的元素_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 如何通过SeleniumWebDriver查找具有动态id的元素

Python 如何通过SeleniumWebDriver查找具有动态id的元素,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我不太熟悉在Python中与Selenium一起使用webdriver,并且在从webportal自动提取数据的过程中遇到了障碍。我试图在文本框中输入一个日期,但我的脚本返回一个NoSuchElementException,无论我尝试以何种方式搜索元素 使用Chrome,我可以使用下面的ID在inspect窗口中轻松识别元素,但是用Python查找它是不可能的 我试图隔离的HTML元素: input id="6A8A7718100001256A44465A5ED3AEAC-toDate" typ

我不太熟悉在Python中与Selenium一起使用webdriver,并且在从webportal自动提取数据的过程中遇到了障碍。我试图在文本框中输入一个日期,但我的脚本返回一个
NoSuchElementException
,无论我尝试以何种方式搜索元素

使用Chrome,我可以使用下面的ID在inspect窗口中轻松识别元素,但是用Python查找它是不可能的

我试图隔离的HTML元素:

input id="6A8A7718100001256A44465A5ED3AEAC-toDate" type="text" value="01/15/2019" size="10" maxlength="10" onchange="validateDateField('to', '6A8A7718100001256A44465A5ED3AEAC-fromDate', '6A8A7718100001256A44465A5ED3AEAC-toDate', '6A8A7718100001256A44465A5ED3AEAC-absRangeErr')"
以下是我尝试过的:

from_date = driver.find_elements_by_id("6A8A7718100001256A44465A5ED3AEAC-fromDate")
from_date = driver.find_element_by_xpath("//input[@id='6A8A7718100001256A44465A5ED3AEAC-fromDate']")
from_date = from_date = driver.find_element_by_css_selector("input[id='6A8A7718100001256A44465A5ED3AEAC-fromDate']")

感谢您的帮助,谢谢

“-toDate”前面的字母数字字符似乎是自动生成的。这意味着它们可能会在站点的构建之间更改,或者在js运行时动态创建

因此,我将在xpath中使用contains关键字,因为它应该对这些更改更加健壮

to_date = driver.find_element_by_xpath("//input[contains(@id, '-toDate')]")
from_date = driver.find_element_by_xpath("//input[contains(@id, '-fromDate')]")
要将日期(即字符序列)发送到文本框中,因为所需元素是动态元素,您必须引导WebDriverWait使该元素可单击,并且您可以使用以下任一解决方案:

  • 使用
    CSS\u选择器

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id$='-toDate'][onchange*='-toDate']"))).send_keys("01/16/2019")
    
  • 使用
    XPATH

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@id, '-toDate') and contains(@onchange, '-toDate')]"))).send_keys("01/16/2019")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
您可以在中找到详细的讨论


sytax看起来不错,但我可以看出您可能使用了错误的id。如果您输入的id是:“6A8A771810001256A44465A5ED3EAC toDate”,为什么要查找这个:“6A8A771810001256A44465A5ED3EAC fromDate”?很好,我有两个文本框(一个是“fromDate”,一个是“toDate”)我正在测试它们是否可以工作。我已经进行了编辑以避免任何进一步的混淆。感谢DebanjanB,异常的链接将我保存在这里。我需要的元素在iframe中,我必须在搜索之前切换到它。感谢cullzie!主要问题是元素在iframe中,我不知道我必须切换到它在寻找嵌套元素之前,请仔细考虑一下这个问题。既然我在正确的窗口中,那么您的解决方案应该会有很大帮助。