【问题标题】:Python, selenium - why do i get this error?Python,硒 - 为什么会出现此错误?
【发布时间】:2021-03-29 18:39:01
【问题描述】:

我想用python访问谷歌地图,但一开始你必须点击接受cookies按钮,我不知道为什么,但我一直收到这个错误:

文件“文件”,第 12 行,在 WebDriverWait(驱动程序, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]'))).click() 文件 "C:\Users\gassp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\support\wait.py", 第 80 行,直到
raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

这是我的代码:

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

driver = webdriver.Chrome('PATH')
url = 'https://www.google.com/maps/'

driver.get(url)

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]'))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()

【问题讨论】:

    标签: python selenium google-maps


    【解决方案1】:

    问题在于它在主框架和接受 cookie 框架之间没有改变。这是解决方案代码:

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome('C:\\Users\\gassp\\OneDrive\\Namizje\\Python.projects\\chromedriver.exe')
    url = 'https://www.google.com/maps/'
    
    driver.get(url)
    
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="consent-bump"]/div/div[1]/iframe')))
    agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]/span/span'))) 
    agree.click()
    
    #back to the main page
    driver.switch_to_default_content()
    
    
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]'))).send_keys('gostilne')
    driver.submit()
    

    【讨论】:

      【解决方案2】:

      当我尝试访问该站点时,我没有在 chrome 开发工具中找到 xpath 定位器 //*[@id="introAgreeButton"]。不确定它指的是什么元素。

      但是,当我评论该行并在搜索按钮上添加点击事件时,我能够得到结果。

      此外,驱动程序实例上没有可用的submit() 方法。

      以下是我使用您提供的代码进行的试用 -

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]'))).send_keys('gostilne')
      WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[@id='searchbox-searchbutton']"))).click()
      

      输出:-

      【讨论】:

      • 我遇到的问题是在那之前。当我运行我的代码时,它会打开一个新的谷歌地图选项卡,但在我没有登录之前我可以做任何事情,我必须点击接受 cookie 按钮,这就是我的问题所在
      • 我刚刚发布了我想要点击这里的图片
      • 将其发布在您的问题本身中,此线程的这一部分仅用于回答
      • @gasper_vaupot 我没有得到那个弹出窗口
      【解决方案3】:

      根据https://selenium-python.readthedocs.io/api.html

      异常 selenium.common.exceptions.TimeoutException(msg=None, screen=None, stacktrace=None) 基础:selenium.common.exceptions.WebDriverException 当命令没有在足够的时间内完成时抛出。

      因此您要么需要增加WebDriverWait(driver, 10) 的时间,要么检查条件EC.element_to_be_clickable 是否可以满足。


      此外,尝试以下实现:

      element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]')))
      element.click()
      

      这用类似的代码结构解决了我的一些问题。

      【讨论】:

      • 我试图将时间增加到 10,但它仍然给我同样的错误。还有其他想法吗?或者错误可能是这个接受cookie窗口是一个弹出窗口?
      • @gasper_vaupot 您指的是隐藏的元素吗? ……可以点击吗?根据您提供给我们的信息,很难为您的问题找到任何类型的解决方案。如前所述,根据 Selenium 官方文档,selenium.common.exceptions.TimeoutException(您收到的错误)指的是以下问题:a command does not complete in enough time
      • 我不明白时间系统是如何工作的。如果我增加数字,等待时间也会增加,对吗?这意味着如果我用 30 而不是 10 更改数字,它应该等待更长时间正确吗?
      • @gasper_vaupot 看看显式等待 (selenium-python.readthedocs.io/waits.html):对于您通过 XPath 提供的元素,声明 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]')))最长处等待 10 秒。如果未找到该元素,则会给出一个TimeoutException(你得到的那个)!所以该元素要么不可点击,要么不存在(可能是错误的 XPath?)。
      • 有没有办法让我可以发送我想要点击的图片?这样你就知道我的意思了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 2022-01-04
      • 2018-12-06
      • 2021-06-01
      • 2019-10-28
      • 2021-01-08
      相关资源
      最近更新 更多