Feitan Portor
Feitan Portor

Reputation: 130

Python - Selenium won't go back to previous page

When accessing the website in variable "url" it automatically accepts cookies, writes the dates and then clicks search, but once it does, 98 applications are shown making it click the first application. I made it so it goes back, redoes it so then it SHOULD click the second application, instead it returns an error.

Can someone try and help make my script go back to the last page and click the second application?

Error:

[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)

Code:

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???")

Upvotes: 1

Views: 3046

Answers (2)

Gihan_G
Gihan_G

Reputation: 159

If you look into the error trace, you will see a StaleElementReferenceException.

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

This error raises because of the web element that you are performing the operation on is no longer attached to the currently loaded DOM. Since you have get the elements at the intial page load and With you navigating back and forth through the web application, those stored web elements are not referring to the currently loaded DOM. Those elements are form the initial page load.

As per my thoughts, the best approach you could follow is to fetch the web element each time so the web elements are attached to the current DOM.

Upvotes: 0

Andersson
Andersson

Reputation: 52665

elements list defined on the main page becomes stale after you leave that page. You can try to replace

elements = driver.find_elements_by_class_name("searchResultsCell")

for e in elements:
    e.click()

with

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()

Also if elements with class name "searchResultsCell" are links, you can do:

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

Upvotes: 5

Related Questions