jeudi 3 septembre 2020

Web scraping python - errors while iterating through web page

I am new in python an I wanted to try web scraping. I am facing a problem with the code bellow. First iteration is working just fine but in second it's not possible for code to take the "Menu" element into a variable. I found similar issue with solution like storing the links in list but I am curious whether would be possible do it this way.

Thank you.

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

enter code here

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time

PATH = "C:\Program Files (x86)\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(PATH)
actions = ActionChains(driver)
driver.get("https://www.mall.sk")


main_menu = driver.find_elements_by_css_selector("li.desktop-menu__item")
main_menu_navi = driver.find_element_by_id("main-menu-navi")

for menu in main_menu:
    actions.move_to_element(main_menu_navi)
    actions.perform()
    main_menu_open = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "main-menu-navi"))
    )
    logo = driver.find_element_by_class_name("head-logo")
    menu.click()
    print("working...")



Aucun commentaire:

Enregistrer un commentaire