尝试查找元素时Chromedriver超时 [英] Chromedriver timesout while trying to locate a element

查看:124
本文介绍了尝试查找元素时Chromedriver超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've deployed a selenium script using python with chromedriver in play on my Heroku server.

驱动程序已正确配置,并且它确实启动了实例,执行了任务.但是,对于随后的调用,驱动程序无法找到该元素-然后再次进行第三次迭代,便能够找到该元素.

The driver is configured properly, and it does launch an instance, performs the task. However for the consequent call, the driver is unable to locate the element - then again for the third iteration, it is able to locate the element.

我不确定明确的睡眠语句是否会起作用.

I'm not quite sure if explicit sleep statement would work.

这是我的硒脚本中的一小段代码:

Here is a small snippet from my selenium script:

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

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

The script is working fine when tested on the local server.

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

Any idea what could have gone wrong here?

推荐答案

我遇到了同样的问题,经过一番挖掘,我发现了

I had the same problem and after some digging I found this post.

因此,很明显,"Google根据您的用户代理呈现了不同的html".我完成了该文章中所示的用户代理设置,但是使用了python:

So, apparently, "Google renders different html based on your user-agent". I did the user agent setup as shown in that post, but in 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")

在那之后一切都很好!

这篇关于尝试查找元素时Chromedriver超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆