Python Selenium在网站中找不到ID

时间:2018-09-07 15:31:17

标签: python selenium

为什么每当我尝试查找“ uxStartDateDecisionTextBox”时,它都说找不到ID。在网站上,他们还有另一个名为“ BodyPlaceHolder_uxTextSearchKeywords”的ID,每当我尝试找到该ID时,它都可以正常工作。有什么问题我尝试使用Google搜索,但没有任何帮助,因此我在这里询问。

代码:

from selenium import webdriver

url = "http://www.fareham.gov.uk/casetrackerplanning/applicationsearch.aspx"

driver = webdriver.Chrome(executable_path=r"C:\Users\Goten\Desktop\chromedriver.exe")
driver.get(url)

driver.find_element_by_id("lnkAllowCookies").click()
driver.find_element_by_id("BodyPlaceHolder_uxLinkButtonShowAdvancedSearch").click()

driver.find_element_by_id("uxStartDateDecisionTextBox").click()
driver.find_element_by_id("uxStartDateDecisionTextBox").clear()
driver.find_element_by_id("uxStartDateDecisionTextBox").send_keys("01/08/2018")

driver.find_element_by_id("uxStopDateDecisionTextBox").click()
driver.find_element_by_id("uxStopDateDecisionTextBox").clear()
driver.find_element_by_id("uxStopDateDecisionTextBox").send_keys("30/08/2018")

driver.find_element_by_id("BodyPlaceHolder_uxButtonSearch").click()

错误:

  Traceback (most recent call last):
File "C:\Users\Goten\Desktop\sel.py", line 11, in <module>
  driver.find_element_by_id("uxStartDateDecisionTextBox").click()
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 359, in find_element_by_id
  return self.find_element(by=By.ID, value=id_)
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
  'value': value})['value']
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
  self.error_handler.check_response(response)
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
  raise exception_class(message, screen, stacktrace)
  selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"uxStartDateDecisionTextBox"}
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

1 个答案:

答案 0 :(得分:1)

在单击“高级搜索”链接之前,整个DOM都不存在整个高级搜索表单。单击链接后,整个表单将在运行时加载到DOM中。因此,除了同步问题之外,我别无其他。 尝试在“ driver.find_element_by_id(“ uxStartDateDecisionTextBox”)。click()”之前添加一个将执行暂停一秒钟左右的步骤。如果之后,Selenium能够找到该元素,则可以确定这是一个同步问题。然后,您可以选择某种显式等待来提高执行速度。