Python-Selenium将不会返回上一页

时间:2018-09-13 12:31:57

标签: python selenium web

当使用变量“ url”访问网站时,它会自动接受cookie,写上日期,然后单击“搜索”,但是一旦完成,将显示98个应用程序,使它单击第一个应用程序。我做到了,它可以返回,重做它,然后应该单击第二个应用程序,而是返回一个错误。

有人可以尝试帮助我使脚本回到最后一页并单击第二个应用程序吗?

错误:

[8796:7240:0913/132540.843:ERROR:install_util.cc(603)] Failed to read HKLM\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmentToken: The system cannot find the file specified. (0x2)

DevTools listening on ws://127.0.0.1:51391/devtools/browser/0f49248f-88a3-48b4-bf7c-975f7b52185a
Worked???
Traceback (most recent call last):
  File "C:\Users\DBaldwin\Desktop\sel.py", line 34, in <module>
    e.click()
  File "C:\Users\DBaldwin\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\DBaldwin\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\DBaldwin\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\DBaldwin\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.16299 x86_64)

代码:

import time
from bs4 import BeautifulSoup
from selenium import webdriver

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

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

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

def rerun():
    driver.find_element_by_id("BodyPlaceHolder_uxLinkButtonShowAdvancedSearch").click()

    time.sleep(3)

    driver.find_element_by_id("uxStartDateDecisionTextBox").click()
    driver.find_element_by_id("uxStartDateDecisionTextBox").clear()
    driver.find_element_by_id("uxStartDateDecisionTextBox").send_keys("1/8/2018")

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

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

    time.sleep(3)

rerun()

elements = driver.find_elements_by_class_name("searchResultsCell")

for e in elements:
    e.click()
    driver.back()
    rerun()
    print("Worked???")

2 个答案:

答案 0 :(得分:2)

离开该页面后,主页上定义的

elements列表将失效。您可以尝试替换

elements = driver.find_elements_by_class_name("searchResultsCell")

for e in elements:
    e.click()

使用

elements_len = len(driver.find_elements_by_class_name("searchResultsCell"))

for index in range(elements_len):
    driver.find_elements_by_class_name("searchResultsCell")[index].click()

如果类名称为"searchResultsCell"的元素是链接,则可以执行以下操作:

links = [link.get_attribute("href") for link in driver.find_elements_by_class_name("searchResultsCell")]
for link in links:
    driver.get(link)

答案 1 :(得分:0)

如果查看错误跟踪,将看到StaleElementReferenceException。

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

由于正在执行操作的Web元素不再附加到当前加载的DOM,因此引发此错误。由于您已在初始页面加载时获得了元素,并且在Web应用程序中来回导航时,那些存储的Web元素并不引用当前加载的DOM。这些元素构成了初始页面加载。

按照我的想法,最好的方法是每次获取Web元素,以便将Web元素附加到当前DOM。