Mod - 38: Exercises - Facing selenium.common.exceptions.StaleElementReferenceException while running the script | Selenium Python Forum
G
Gaurav Valera Posted on 10/01/2021

Hi, 

Please guide me to remove the error

Code:

from selenium import webdriver
import time

# Dynamic browser code
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

from selenium.webdriver.support.wait import WebDriverWait

browserName = 'Chrome'
webdriver.driver = None

if browserName == 'Chrome':
    driver = webdriver.Chrome(executable_path='C:/Git_Data/automationFrameworks/chromedriver.exe')

elif browserName == 'Firefox':
    driver = webdriver.Firefox(executable_path='C:/Git_Data/automationFrameworks/geckodriver.exe')

elif browserName == 'edge':
    driver = webdriver.Edge(executable_path='C:\\Git_Data\\automationFrameworks\\msedgedriver.exe')

elif browserName == 'IE':
    driver = webdriver.Ie(executable_path='C:/Git_Data/automationFrameworks/IEDriverServer.exe')


def clickAndWait(xpathExpTarget, xpathExpWait, maxTime):
    for i in range(0, maxTime):
        driver.find_element_by_xpath(xpathExpTarget).click()

        if isElementPresent(xpathExpWait) and driver.find_element_by_xpath(xpathExpWait).is_displayed():
            return
        else:
            time.sleep(1)


def isElementPresent(xpathExp):
    s = driver.find_elements_by_xpath(xpathExp)

    if len(s) == 0:
        return False
    else:
        return True


def deleteStockTest():
    driver.set_page_load_timeout(10)
    driver.find_element_by_xpath("//a[@id='deletePortfolio']").click()
    driver.switch_to.alert.accept()
    driver.switch_to.default_content()


driver.maximize_window()
driver.implicitly_wait(10)
driver.get('https://www.rediff.com/')

# login code
driver.find_element_by_xpath("//*[@class='logobar']/div/div[2]/a[2]").click()
driver.find_element_by_css_selector('span#signin_info > a:nth-child(1)').click()
driver.find_element_by_id('useremail').send_keys('abhiguleria143@gmail.com')
wait = WebDriverWait(driver, 10)
passwordField = wait.until(EC.presence_of_element_located((By.ID, 'userpass')))
passwordField.send_keys('Abhijeet@35')
# driver.find_element_by_id('userpass').send_keys('Abhijeet@35')
login_submit = wait.until(EC.presence_of_element_located((By.ID, 'loginsubmit')))
login_submit.click()

# Add profile
clickAndWait(xpathExpTarget="//*[@id='createPortfolio']/img", xpathExpWait="//*[@id='createPortfolioButton']",
             maxTime=10)
driver.find_element_by_css_selector('input#create').clear()
driver.find_element_by_css_selector('input#create').send_keys('gr65')
submit = wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id = 'createPortfolioButton']")))
submit.click()

# delete portfolio
deleteStockTest()

time.sleep(5)
driver.quit()

Output:

Traceback (most recent call last):
  File "C:\Git_Data\automationFrameworks\SelFinal\rediff_Exercise\createProfile.py", line 77, in <module>
    deleteStockTest()
  File "C:\Git_Data\automationFrameworks\SelFinal\rediff_Exercise\createProfile.py", line 48, in deleteStockTest
    driver.find_element_by_xpath("//a[@id='deletePortfolio']").click()
  File "C:\Users\gaura\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\gaura\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\gaura\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\gaura\AppData\Local\Programs\Python\Python39\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=87.0.4280.141)

0
09914040666 Replied on 12/01/2021

Hey,

The reason of getting this exception is that when you are creating the portfolio, the page refreshes to update the list of portfolios available because of which only the delete button is not able to be found by selenium. There are javascript function applied on the delete button as well because of which explicit wait, implicit wait or page load timeout does not work. 

So to overcome the error we will have to wait, we can include time.sleep(2) in the deleteStockTest() and then the code will working fine and the created portfolio will be deleted as well.


G
Gaurav Valera Replied on 12/01/2021

Thanks, it was working with time.sleep()

 


0
09914040666 Replied on 12/01/2021

Thank you for the update. 


Related Posts