简体   繁体   English

如何在python3中使用Selenium和phantomjs等待网页截图之前?

[英]How to wait before screenshot of webpage using selenium and phantomjs in python3?

Would like to create an image of a webpage but must wait to allow it to fully load (inc. AJAX). 想创建网页图像,但必须等待它完全加载(包括AJAX)。

The wait time should be set by an hardcoded value rather than waiting on an element. 等待时间应由硬编码值设置,而不要等待元素。

The following takes a screenshot with no wait: 以下是无需等待的截图:

from selenium import webdriver

driver=webdriver.PhantomJS()
driver.set_window_size(w,h)
driver.get('webpage_address')
driver.save_screenshot('/path/to/dir/foo.png')
driver.quit()

Based on some documentation : 根据一些文档

from selenium import webdriver

driver=webdriver.PhantomJS()
driver.set_window_size(w,h)
driver.implicitly_wait(t)
driver.get('webpage_address')
driver.save_screenshot('/path/to/dir/foo.png')
driver.quit()

However, this does not output an image nor does it seem to be waiting. 但是,这不会输出图像,也不会等待图像。

Any help is much appreciated! 任何帮助深表感谢!

Try the following code: 尝试以下代码:

driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
or
wait = WebDriverWait(browser, 10)

Here we are implicitly asking the code to waiting for 10 seconds 在这里,我们隐式地要求代码等待10秒

Perhaps waiting until an element is present on the given page, which would mean it's fully loaded (not in all cases) you can then proceed to take a screenshot :) 也许等到某个元素出现在给定页面上,这意味着它已完全加载(并非在所有情况下),然后可以继续截图:)

WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement"))

driver.save_screenshot('/path/to/dir/foo.png')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM