Home > Software design >  Selenium Returning Error in Python For Loop, and Recursion
Selenium Returning Error in Python For Loop, and Recursion

Time:02-06

I am attempting to use selenium to go through a list of strings, and search every element of the list using Google Chrome.

The following is the code that I have created, using recursion:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome(executable_path="/Applications/Google_Chrome.app")
driver.implicitly_wait(0.5)

file1 = open('questions.txt', 'r')
File = file1.readlines()

driver.get("https://www.google.com/")
m = driver.find_element("name", "q")

def runSearches(list):
    if list:
        m = driver.find_element("name", "q")
        m.send_keys(list[0])
        time.sleep(0.2)
        m.send_keys(Keys.ENTER)
        time.sleep(0.2)
        driver.back()
        time.sleep(0.2)
        return runSearches(list[1:])
    else:
        print("done")

runSearches(File)

When I run this code, it works for the first item in the list. However, after this search occurs, selenium ignores the driver.back() and doesn't go back a page, and returns the following:

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

I understand the cause of this issue. The page isn't going back, and thus selenium is looking for an element which doesn't exist on the current page. What I don't understand is why selenium is not going back a page. No matter how long I make the sleep time between commands, the same result always occurs. Utilizing driver.get("https://www.google.com/") instead of driver.back() does not work.

I have tried to implement an iterative version of this function using a for loop, with no sucess or difference in outcome.

CodePudding user response:

This is a common issue and happens when selenium was able to find the element but not able to locate it anymore. You simply need to handle the exception and capture the element again.

def runSearches(list):
    if list:
        m = None
        for _ in range(5):
            try:
                m = driver.find_element("name", "q")
                m.send_keys(list[0])
                time.sleep(0.2)
                m.send_keys(Keys.ENTER)
                time.sleep(0.2)
                driver.back()
                time.sleep(0.2)
                return runSearches(list[1:])
            except StaleElementReferenceException:
                m = driver.find_element("name", "q")


    else:
        print("done")

You need to import the exception in you script:

from selenium.common.exceptions import StaleElementReferenceException

You could also consider using wait while finding the elements as they're not guaranteed to load immediately as the page loads.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 15)
m = wait.until(EC.presence_of_element_located((By.NAME, 'q')))
  • Related