【问题标题】:Chromedriver timesout while trying to locate a element尝试定位元素时 Chromedriver 超时
【发布时间】:2019-06-19 06:29:19
【问题描述】:

我已经在我的 Heroku 服务器上使用 python 和 chromedriver 部署了一个 selenium 脚本。

编辑:驱动程序配置正确,它会启动一个实例,执行任务。然而,对于随后的调用,驱动程序无法定位该元素 - 然后再次进行第三次迭代,它能够定位该元素。

我不太确定显式睡眠语句是否有效。

这是我的 selenium 脚本中的一个小 sn-p:

def execute_entry(url,proxy,email):
      try:
            browser = driver.load_chrome_driver(proxy)
            browser.get(url)
            browser.maximize_window()

            browser.execute_script("window.scrollTo(0, 1040)")
            sleep(5)
            print("Launching browser")

            # check if the page is still available
            try:
                  print("Checking for page")
                  element_present = expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="comp_firstname"]'))
                  WebDriverWait(browser, 20).until(element_present)
            except Exception as e:
                  # exit script
                  logging.exception("message")
                  print("Unable to locate element")
                  message = "There seems to be an issue with your registration. Entry is probably closed or their server is not responsive at the moment. Timed out waiting for page to load. Please try again later!"
                  email_ops.send_email(email, 'Entry Status: Error', message)
                  browser.quit()
                  return False

            # register - if page is available
            .....
            .....
            # rest of the selenium script to fill the form details

在本地服务器上测试时脚本运行良好。

知道这里可能出了什么问题吗?

【问题讨论】:

  • 又试了几次,好像没有图案了。解决方案不稳定。
  • 我自己也有类似的问题。抓取被网站拒绝,我尝试使用代理并更改用户代理标头,或者同时花费了太多时间或全部时间。它在本地也工作得更好,但在 Heroku 中部署时一切似乎都变得更加困难。
  • 我遇到了同样的问题 - 但只有我的一个网络抓取工具。它在我的 Mac 上完美运行,但每次在 Heroku 上都无法检测完成登录过程后新页面何时加载。与其他基于 Heroku 的网络抓取工具相同的登录过程。不知所措地解决问题。
  • 嘿,你有没有想过这个问题?我已经尝试了所有方法,无论我尝试使用何种参数或环境变量排列,在尝试打开 URL、加载页面并等待元素显示时都会超时。在我的 macbook 上运行良好。

标签: python heroku selenium-chromedriver


【解决方案1】:

我遇到了同样的问题,经过一番挖掘,我找到了this post

因此,显然,“Google 会根据您的用户代理呈现不同的 html”。我做了用户代理设置,如那篇文章所示,但在 python 中:

chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36")

之后一切正常!

【讨论】:

  • 欢迎使用 stackoverflow。通常,答案应该是自包含的:链接可以更改,但即使链接断开,答案也必须有效。如果情况还不是这样 - 您是否可以编辑您的答案,以便此处包含所有信息并且断开的链接不会影响它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 2019-08-08
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多