随机弹出窗口中的Python Selenium Click Intercept错误

问题描述

您好,我使用的是自动网页抓取工具,但随机情况下,屏幕上会显示一个接受cookie弹出窗口,该弹出窗口阻止了表单单击按钮。它会影响整个脚本,我不知道如何写以及cookie_pop_up.close()或类似性质的东西。获取关闭的xpath以获取Cookie?

我正尝试登录snapchat,这是我的代码:

hrome_options = webdriver.ChromeOptions()
                chrome_options.add_argument(f"--proxy-server=http://{random.choice(live_proxies)}")
                driver = webdriver.Chrome("chromedriver.exe",options=chrome_options)
                driver.set_window_position(-10000,0)
                driver.get("https://accounts.snapchat.com/accounts/login")
                if "Log in to Snapchat" in driver.page_source:
                    proxy_is_valid = True
                    user_input = driver.find_element_by_id("username")
                    user_input.send_keys(user)

                    passsword_input = driver.find_element_by_id("password")
                    passsword_input.send_keys(password)

                    login_button = driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/article/div[1]/div/form/div[4]/button")
                    login_button.click()
                    if check_for_captcha_connectivity(driver):

                        solve(driver)

这是错误:

File "C:\Users\Administrator\Desktop\python\lib\site-packages\selenium\webdriver\remote\webelement.py",line 633,in _execute
    return self._parent.execute(command,params)
  File "C:\Users\Administrator\Desktop\python\lib\site-packages\selenium\webdriver\remote\webdriver.py",line 321,in execute
    self.error_handler.check_response(response)
  File "C:\Users\Administrator\Desktop\python\lib\site-packages\selenium\webdriver\remote\errorhandler.py",line 242,in check_response
    raise exception_class(message,screen,stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="submit" class="btn btn-lg btn-primary">...</button> is not clickable at point (507,384). Other element would receive the click: <div class="cookie-popup">...</div>
  (Session info: chrome=77.0.3865.75)

在这里看起来像阻止登录一样: https://imgur.com/gallery/sZroFq9

提前谢谢!

更新我尝试过

login_button = driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/article/div[1]/div/form/div[4]/button")
                    login_button.click()
                    if ElementClickInterceptedException:
                        time.sleep(1)
                        driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/div/div/div[3]/div[4]").click()
                        continue

仍然无法使怪异的粘贴缩进^

解决方法

您需要先同意Cookie同意,然后再继续。要接受Cookie同意,您需要为element_to_be_clickable()引入WebDriverWait,并且可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.cookie-popup"))).click()
    driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/article/div[1]/div/form/div[4]/button").click()
    
  • 使用XPATH

    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='cookie-popup']"))).click()
    driver.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/article/div[1]/div/form/div[4]/button").click()
    
  • 注意:您必须添加以下导入:

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...