1

I've been trying to scrape some information of this E-commerce website with selenium. However when I access the website I need to accept cookies to continue. This only happens when the bot accesses the website, not when I do it manually. When I try to find the corresponding element either by xpath, as I find it when I inspect the page manually I always get this error message:

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

My code is mentined below.

import time
import pandas
pip install selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup #pip install beautifulsoup4

PATH = "/Users/Ziye/Desktop/Python/chromedriver"
delay = 15

driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)

driver.get("https://en.zalando.de/women/?q=michael+michael+kors+taschen")
driver.find_element_by_xpath('//*[@id="uc-btn-accept-banner"]').click()

This is the HTML corresponding to the button "That's ok". The XPATH is as above.

<button aria-label="" id="uc-btn-accept-banner" class="uc-btn uc-btn-primary">
                          That’s OK <span id="uc-optin-timer-display"></span>
                      </button>

Does anyone know where my mistake lies?

0

1 Answer 1

1

You should add explicit wait for this button:

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


driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.implicitly_wait(10)

driver.get("https://en.zalando.de/women/?q=michael+michael+kors+taschen")
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="uc-btn-accept-banner"]')))
driver.find_element_by_xpath('//*[@id="uc-btn-accept-banner"]').click()

Your locator is correct. As css selector, you can use .uc-btn-footer-container .uc-btn.uc-btn-primary

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.