Home > database >  Message: stale element reference: element is not attached to the page document after returning a pag
Message: stale element reference: element is not attached to the page document after returning a pag

Time:01-01

Currently I am creating a python script which go on https://www.mwcbarcelona.com/exhibitors and click on each exhibitors and return to same page and then click next exhibitors. I write a code:

from bs4 import BeautifulSoup
import requests
import csv
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome("chromedriver.exe")
driver.get('https://www.mwcbarcelona.com/exhibitors')

td_list = driver.find_elements_by_css_selector("tr[class='flex px-4 cursor-pointer hover:bg-gray-100 sm:table-row sm:text-gray-700 sm:font-medium']")

for desc in td_list:
    print(desc.text)
    desc.click()
    time.sleep(3)
    driver.back()
    time.sleep(3)

when I run my code I can go only first exhibitors then it gives me this error:

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

Can Someone tell me what is wrong here.

CodePudding user response:

wait=WebDriverWait(driver,10)                                 

driver.get("https://www.mwcbarcelona.com/exhibitors")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#onetrust-accept-btn-handler"))).click()
i=1
while True:
    try:
        elem=wait.until(EC.element_to_be_clickable((By.XPATH,f"(//tr[@class='flex px-4 cursor-pointer hover:bg-gray-100 sm:table-row sm:text-gray-700 sm:font-medium'])[{i}]")))
        driver.execute_script("arguments[0].click();", elem)
        driver.back()
        i =1
    except Exception as e:
        print(str(e))
        break

When you go away from a page you lose all your old elements. If it was a tags you could collect hrefs otherwise you need to track the index. Also it seems img was the one getting the click so you could deal with that however you want.

Imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

CodePudding user response:

Seems like td_list became inaccessible inside the for loop due to the performed action(navigation). DOM is changing that is why you are getting stale element reference exception. Intialise the element "td_list" inside the for loop.

you can also use explicit wait condition to wait until the element is available.

  • Related