【问题标题】:Selenium GDPR NoSuchElementExceptionSelenium GDPR NoSuchElementException
【发布时间】:2020-08-13 21:40:31
【问题描述】:

我想从“https://www.techadvisor.co.uk/review/wearable-tech/”中抓取一些数据。我发现用 Beautifulsoup 循环浏览页面是行不通的。这就是我尝试用硒打开它的原因。无法找到克服 GDPR 拦截器的“全部接受”按钮。

我试过了:

browser = webdriver.Chrome()
browser.get("https://www.techadvisor.co.uk/review/wearable-tech/")
# button = browser.find_element_by_xpath('/html/body/div/div[3]/div[5]/button[2]')
# WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "html/body/div/div[3]/div[5]/button[2]"))).click()

我总是收到 NoSuchElementException

说实话,我发现 Xpath 真的很奇怪,但我是从 Google Chrome 检查中得到的。

感谢每一个解决方案建议或提示 :)

【问题讨论】:

    标签: python selenium nosuchelementexception


    【解决方案1】:

    点击iframe内的Accept All按钮。您需要先切换到iframe才能点击按钮。

    诱导WebDriverWait() 并等待frame_to_be_available_and_switch_to_it() 并使用以下css选择器。

    诱导 WebDriverWait() 并等待 element_to_be_clickable() 并使用以下 xpath 选择器。

    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
    
    browser = webdriver.Chrome()
    browser.get("https://www.techadvisor.co.uk/review/wearable-tech/")
    WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='sp_message_iframe']")))
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept All']"))).click()
    

    【讨论】:

      【解决方案2】:

      我知道这个问题很老了, 但我想提供我自己的解决方案! 第一步是识别您实际查看的表单的“id”,然后您需要将焦点移到它上面!

      driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="gdpr-consent-notice"]'))
      
          cookies = driver.find_element_by_xpath('/html/body/app-root/app-theme/div/div/app-notice/app-theme/div/div/app-home/div/div[3]/div[2]/a[3]/span')
      
          cookies.click()
      

      【讨论】:

        猜你喜欢
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2021-02-19
        • 2020-04-24
        • 1970-01-01
        • 2019-03-26
        • 2021-03-31
        • 2013-11-24
        相关资源
        最近更新 更多