在 Python 中单击带有 Selenium 的“接受 Cookie”弹出窗口
Click “Accept Cookies” popup with Selenium in Python
我一直在尝试用 selenium 抓取这个电子商务网站的一些信息。但是,当我访问该网站时,我需要接受 cookie 才能继续。这仅在机器人访问网站时发生,而不是在我手动访问网站时发生。当我尝试通过 xpath 找到相应的元素时,正如我在手动检查页面时发现的那样,我总是收到此错误消息:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
我的代码如下所示。
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()
这是按钮“That's ok”对应的HTML。 XPATH如上。
<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>
有谁知道我的错误在哪里?
您应该添加明确等待此按钮:
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()
您的定位器是正确的。
作为 css 选择器,您可以使用 .uc-btn-footer-container .uc-btn.uc-btn-primary
我一直在尝试用 selenium 抓取这个电子商务网站的一些信息。但是,当我访问该网站时,我需要接受 cookie 才能继续。这仅在机器人访问网站时发生,而不是在我手动访问网站时发生。当我尝试通过 xpath 找到相应的元素时,正如我在手动检查页面时发现的那样,我总是收到此错误消息:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
我的代码如下所示。
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()
这是按钮“That's ok”对应的HTML。 XPATH如上。
<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>
有谁知道我的错误在哪里?
您应该添加明确等待此按钮:
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()
您的定位器是正确的。
作为 css 选择器,您可以使用 .uc-btn-footer-container .uc-btn.uc-btn-primary