硒隐式等待不起作用 [英] selenium implicitly wait doesn't work

查看:109
本文介绍了硒隐式等待不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用硒和无头浏览器,因为我想使用ajax技术抓取某些网页.

This is the first time I use selenium and headless browser as I want to crawl some web page using ajax tech.

效果很好,但是在某些情况下,加载整个页面会花费太多时间(尤其是当某些资源不可用时),因此我必须为硒设置超时时间.

The effect is great, but for some case it takes too much time to load the whole page(especially when some resource is unavailable),so I have to set a time out for the selenium.

首先我尝试了set_page_load_timeout()set_script_timeout(),但是当我设置这些超时时,如果页面未完全加载,我将不会得到任何页面源,如下代码:

First of all I tried set_page_load_timeout() and set_script_timeout(),but when I set these timeouts, I won't get any page source if the page doesn't load completely, as the codes below:

driver = webdriver.Chrome(chrome_options=options)
driver.set_page_load_timeout(5)
driver.set_script_timeout(5)
try:
    driver.get(url)
except Exception:
    driver.execute_script('window.stop()')

print driver.page_source.encode('utf-8')  # raise TimeoutException this line.

所以我尝试使用隐式等待和条件等待,如下所示:

so I try to using Implicitly Wait and Conditional Wait, like this:

driver = webdriver.Firefox(firefox_options=options, executable_path=path)
print("Firefox Headless Browser Invoked")
wait = WebDriverWait(driver, timeout=10)
driver.implicitly_wait(2)
start = time.time()
driver.get(url)
end = time.time()
print 'time used: %s s' % str(end - start)
try:
    WebDriverWait(driver, 2, 0.5).until(expected.presence_of_element_located((By.TAG_NAME, 'body')))
    print driver.find_element_by_tag_name('body').text
except Exception:
    driver.execute_script('window.stop()')

这一次我获得了想要的内容.但是,这需要很长时间(40+秒),这意味着我设置的2秒钟的超时时间根本不起作用.

This time I got the content that I want.However,it takes a very long time(40+ seconds),that means the timeout I set for 2 seconds doesn't work at all.

在我看来,在浏览器停止加载页面之前,driver.get()调用似乎结束了,只有在下面的代码可以正常工作之后,您才能终止get()调用,否则您将一无所获. 但这与硒文档有很大不同,我真的很想知道错误在哪里.

In my view, it seems like the driver.get() call ends until the browser stop loading the page, only after that the codes below can work, and you can not kill the get() call or you'll get nothing. But this is very different from the selenium docs, I REALLY wonder where is the mistake.

环境:OSX 10.12,带有FireFox& amp;的硒3.0.9. GoogleChrome Headless(均为最新版本.)

environment: OSX 10.12, selenium 3.0.9 with FireFox & GoogleChrome Headless(both latest version.)

-更新----

感谢您的帮助.我仅使用WebDriverWait()来更改代码,但仍然存在呼叫持续很长时间的情况,远远超过了我设置的超时时间. 想知道我是否可以在超时后立即停止页面加载?

Thanks for help.I change the code as below, using WebDriverWait() alone, but there still exist cases that the call last for a very long time, far more than the timeout that I set. Wonder if I can stop the page load immediately as the time is out?

driver = webdriver.Firefox(firefox_options=options, executable_path=path)
print("Firefox Headless Browser Invoked")
start = time.time()
driver.get('url')
end = time.time()
print 'time used: %s s' % str(end - start)
try:
    WebDriverWait(driver, 2, 0.5).until(expected.presence_of_element_located((By.TAG_NAME, 'body')))
    print driver.find_element_by_tag_name('body').text
except Exception:
    driver.execute_script('window.stop()')
driver.quit()

这是测试中的终端输出:

Here is a terminal output in test:

Firefox Headless Browser Invoked
time used: 44.6049938202 s

根据代码,这意味着driver.get()调用需要44秒才能完成调用,这是意外的,我想知道我是否误解了无头浏览器的行为?

according to the code this means the driver.get() call takes 44 seconds to finish call, which is unexpected,I wonder if I misunderstood the behavior of the headless browsers?

推荐答案

就像您在问题中提到的那样加载整个页面需要花费太多时间(尤其是当某些资源不可用时)很漂亮如果待测应用程序( AUT )使用 JavaScript AJAX调用,则很有可能.

As you mentioned in your question it takes too much time to load the whole page(especially when some resource is unavailable) is pretty much possible if the Application Under Test (AUT) uses JavaScript or AJAX Calls.

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