Selenium-Webdriver Ruby->单击后如何等待图像完全加载 [英] Selenium-Webdriver Ruby --> How to wait for images to be fully loaded after click

查看:139
本文介绍了Selenium-Webdriver Ruby->单击后如何等待图像完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby和Selenium-Webdriver的新手,请帮助:)

I am very new to Ruby and Selenium-Webdriver, so please, help :)

我正在尝试打开发送到我的收件箱的电子邮件活动,该活动包含图像并在Firefox中进行截图.但是我不能等到图像完全加载.一旦我单击显示图像",就已经截取了屏幕截图,但当时未加载图像.在显示所有图像之后,如何在以后的某个时间暂停脚本并拍摄屏幕截图?

I am trying to open email campaign , sent to my inbox, that has images and take a screenshot in the firefox. But i can not make it wait until images is fully loaded. Once i click on 'Show images' , screenshot is already taken , but image is not loaded at that time. How can i pause the script and take screenshot some time later, after all images is displayed?

请帮助:(

贝勒是我的剧本:

enter code here

require 'selenium-webdriver'

browser = Selenium::WebDriver.for :firefox

#==========================================================================================

wait = browser.manage.timeouts.implicit_wait = 15
#==========================================================================================
url = 'https://login.yahoo.com/config/login_verify2?.intl=us&.src=ym'
# Open browser (firefox)
browser.navigate.to url
browser.find_element(:id, 'username').send_keys "some yahoo id"
browser.find_element(:id, 'passwd').send_key "some password"
browser.find_element(:id, ".save").click
browser.find_element(:id, "inbox-label").click
browser.find_element(:xpath, "//div[@class='subj']").click
browser.find_element(:xpath, "//a[@title='Display blocked images']").click

result_page_title = browser.find_element(:tag_name, 'title')
    puts "Title of the page: \t\t: #{result_page_title.text}"
    browser.save_screenshot "1.jpg"

推荐答案

您可以使用Implicit Wait and Explicit Wait等待特定的Web元素,直到它出现在页面中.您可以定义等待时间,具体取决于应用程序.

You can use Implicit Wait and Explicit Wait to wait for a particular Web Element until it appears in the page. The wait period you can define and that is depends upon the application.

明确等待:

显式等待是您定义的代码,用于在继续执行代码之前等待特定条件发生.如果满足条件,它将终止等待并继续执行其他步骤.

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. If the condition achieved it will terminate the wait and proceed the further steps.

代码:

 WebDriverWait wait = new WebDriverWait(driver,30);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(strEdit)));

 WebElement myDynamicElement = (new WebDriverWait(driver, 30))
 .until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
    return d.findElement(By.id("myDynamicElement"));
}});

此方法最多等待30秒,然后抛出TimeoutException,否则如果发现该元素将在0-30秒内返回它.默认情况下,WebDriverWait每500毫秒调用ExpectedCondition,直到成功返回为止.对于ExpectedCondition类型,成功返回是Boolean返回true,对于其他所有ExpectedCondition类型,返回值都是非null.

This waits up to 30 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 30 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

您可以根据需要为应用程序使用ExpectedConditions类.

You can use ExpectedConditions class as you need for the application.

隐式等待:

隐式等待是告诉WebDriver在尝试查找一个或多个元素(如果它们不立即可用)时轮询DOM一定时间

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available

代码:

 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

要记住的一件事是,一旦设置了隐式等待,它将在WebDriver对象实例的生存期内保持不变

One thing to keep in mind is that once the implicit wait is set - it will remain for the life of the WebDriver object instance

有关更多信息,请使用此链接 http://seleniumhq.org/docs/04_webdriver_advanced.jsp

For more info use this link http://seleniumhq.org/docs/04_webdriver_advanced.jsp

上面的代码在Java中.根据您的语言需求进行更改.

The above code is in Java. Change as your language need.

这篇关于Selenium-Webdriver Ruby->单击后如何等待图像完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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