不久前,我写了一个简单的刮板,打开了chrome浏览器,并从一个网站上抓取了一些数据。然而,现在每当我运行这个脚本时,它都不会打开我提供的url,而是重定向到GDPR同意网站。我从选项中删除了--匿名模式,但它仍然是一样的。铬打开后脚本崩溃,因为它是自动重定向到该GDPR同意网页。我该如何处理这个问题呢?
下面是再现错误的代码。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
option = webdriver.ChromeOptions()
#option.add_argument("--incognito")
browser = webdriver.Chrome(executable_path='chromedriverpath', chrome_options=option)
rval=[]
browser.get("https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL")
timeout = 10
WebDriverWait(browser, timeout)
values_element = browser.find_elements_by_xpath("//td[@class='Fz(s) Fw(500) Ta(end)']")
print(browser)
values = [x.text for x in values_element]
rval.append(values[8])
for title, value in zip(stockname, rval):
print(title + ': ' + value)
evdict=dict(zip(stockname, rval))发布于 2019-07-02 13:01:19
因此,为了绕过这样一个弹出窗口,该窗口阻止selenium抓取我需要添加的数据:
browser.find_element_by_xpath("//input[@type='submit' and @value='OK']").click()它会点击正确的按钮并为我关闭窗口。然后Selenium工作,没有进一步的问题。
https://stackoverflow.com/questions/51157815
复制相似问题