明确等待Selenium Webdriver [英] Explicit wait for Selenium Webdriver

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

问题描述

正在研究试图理解显式等待的方法.

Working on the method trying to understand the explicit wait.

    require 'rubygems'
require 'selenium-webdriver'
require 'cucumber'

$driver = Selenium::WebDriver.for :firefox
$driver.manage.timeouts.implicit_wait = 3

    Then /^do search$/ do
  driver = $driver
  one_way = driver.find_element(:id, "search.ar.type.code.oneWay").click
    sleep 5
   from = driver.find_element :xpath => "//div[@class = 'origin column1']//input[@type = 'text']"
   from.click

因此,在单击one_way单选按钮并更改了输入格式后,我放置了sleep 5使其出现一个时间元素,否则将出现错误元素不可见...".因此,我认为这是理解显式等待的好时机,因为我需要等待直到元素出现.

So after one_way radio button is clicked and input form changed , so I put sleep 5 to give it a time element to appear, otherwise would be error "element not visible ...". So I thought it would be good time to understand explicit wait, because I need to wait till element will appear.

    wait = Selenium::WebDriver::Wait.new(:timeout => 40)
  wait.until {from = driver.find_element(:xpath, "//div[@class = 'origin column1']//input[@type = 'text']")
  from.click
  }

但是出现错误"Selenium :: WebDriver :: Error :: ElementNotVisibleError:元素当前不可见,因此可能无法与之交互".为什么此代码不等到元素出现并单击它?

But getting error "Selenium::WebDriver::Error::ElementNotVisibleError: Element is not currently visible and so may not be interacted with" . Why this code doesn't wait till element appears and click it?

推荐答案

问题是该元素尚未在DOM中,因此您需要在其中放置时间延迟.

The issue is that the element isn't in the DOM yet, hence your needing to put a time delay in there.

也就是说,API 针对红宝石的doco 说您应该这样做

That said, the API doco for ruby says you should do it this way

require 'rubygems' # not required for ruby 1.9 or if you installed without gem
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get "http://somedomain/url_that_delays_loading"

wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
begin
  element = wait.until { driver.find_element(:id => "some-dynamic-element") }
ensure
  driver.quit
end

请注意,您随后可以.click()使用的元素是通过wait.until方法分配的,而不是代码中的find_element()方法.

note that the element that you can then .click() is assigned from the wait.until method not the find_element() method as in your code.

但是,任意延迟并不总是有效,如果站点繁忙,则延迟可能不够长. 更好的选择是等待元素变为可单击或可见.

However that arbitrary delays don't always work, if the site is busy then the delay may not be long enough. A better option is to wait for the element to become clickable or visible.

Java API具有 cn这样使用的ExpectedConditions便利方法 ...

The Java API's have ExpectedConditions convenience methods that cn be used like this...

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
element.click();

不幸的是,我认为Ruby还没有这个功能.您可能需要编写自己的ExpectedCondition类或程序包.

Unfortunatly I don't think Ruby has this yet. You may need to code up your own ExpectedCondition class or package.

我不是Ruby开发人员,但这是Java中可以用来实现ExpectedCondition的功能的想法

I'm not a Ruby dev but here is the idea of a function in Java that could be used to implement your ExpectedCondition

public WebElement findElementInTime(WebDriver driver, By by, int timeoutInSeconds)
    {
        log.debug("==================+>>>>>> looking for element for "+ timeoutInSeconds + " seconds");
        WebElement ret = null;

        for (int second = 0; second < timeoutInSeconds; second++) {
            try { if (isElementPresent(driver,by)) {
                log.debug("found element :-)");
                ret = driver.findElement(by);
                break;
            }} catch (Exception e) {

                log.debug("oops... sleeping 1 sec wait for button");
            }
            log.debug("sleeping 1 sec wait for button");
        }
        return ret;
    }

这篇关于明确等待Selenium Webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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