Reputation: 45
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.
Upvotes: 0
Views: 90
Reputation: 9969
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
Upvotes: 1