webdriver的不可靠单击链接或按钮 [英] WebDriver does not reliably click links or buttons

查看:435
本文介绍了webdriver的不可靠单击链接或按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在拼命地试图获得的webdriver点击一个按钮或链接可靠,但它只是不希望合作。我试图从设置一个隐含的超时时间,以低于code这是假设点击并等待元素出现不同的方法。

code的下面片段被发现的地方在互联网上,它最接近我来可靠地获得一个按钮或链接点击。但它不工作同样在调试模式下,它不会,当​​我夜间的回归测试过程中执行。

有谁知道另一种方法单击浏览器按钮或链接的?或者我应该使用硒1,不作为的webdriver这是太新,可靠地使用。

 公共无效waitAndClick(webdriver的驱动程序,通过用){
    WebDriverWait等待=新WebDriverWait(驱动程序,10000,2000年);
    功能< webdriver的,布尔> waitForElement =新waitForElement(所);
    wait.until(waitForElement);    操作建设者=新的动作(驱动器);
    builder.click(driver.findElement(所))
            .perform();
}私有类waitForElement实现了功能与LT; webdriver的,布尔> {
    私人最终通过通过;    私人字符串文本= NULL;    公共waitForElement(通过用){
        this.by =通过;
    }    公共waitForElement(通过用,字符串文本){
        this.by =通过;
        this.text =文本;
    }    @覆盖
    公共布尔申请(从的webdriver){
        如果(this.text!= NULL){
            对于(WebElement E:from.findElements(this.by)){
                如果(e.getText()。等于(this.text)){
                    返回Boolean.TRUE;
                }
            }            返回Boolean.FALSE;
        }其他{
            尝试{
                。driver.switchTo()defaultContent()SWITCHTO()框架(顶)。;
                from.findElement(this.by);
            }赶上(例外五){
                logger.error(无法找到\\+ this.by.toString()+\\重试......);
                返回Boolean.FALSE;
            }
            logger.info(找到\\+ this.by.toString()+\\);
            返回Boolean.TRUE;
        }
    }
}

控制台出在Eclipse中调试模式:

  16:07:08109信息WebDriverUtility:适用找到By.linkText:类。
16:07:10514信息WebDriverUtility:适用找到By.linkText:报告。
16:07:17028错误WebDriverUtility:申请无法找到By.linkText:用户。重试....
16:07:26369信息WebDriverUtility:适用找到By.linkText:用户。
16:07:38272错误WebDriverUtility:申请无法找到By.linkText:系统。重试....
16:07:41,334 INFO WebDriverUtility:适用找到By.linkText:系统。
16:07:47722错误WebDriverUtility:申请无法找到By.linkText:学校。重试....
16:07:50565信息WebDriverUtility:适用找到By.linkText:学校。

控制台出从Eclipse中运行时:

  16:14:04179信息WebDriverUtility:适用找到By.linkText:类。
16:14:04726信息WebDriverUtility:适用找到By.linkText:报告。
16:14:09771信息PageAPITesting:登录org.openqa.selenium.NoSuchElementException:无法找到链接文本元素==报告(警告:服务器未提供任何信息的堆栈跟踪)
有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html
构建信息:版本:2.0rc3,修订版:'12536',时间:2011-06-20 18点19分52秒
系统信息:os.name:Windows XP中,os.arch:'86',os.version:'5.1',java.version:1.6.0_24
驱动方式:driver.version:RemoteWebDriver
16:14:09865信息PageAPITesting:登录当前选项卡中的类
16:14:09958信息WebDriverUtility:适用找到By.linkText:学校。
16:14:10,240 INFO PageAPITesting:登录java.lang.IllegalStateException:无法导航到该页面ca.schoolspecialty.qa.api.pages.schools.MenuSchoolPage


解决方案

我没有看到任何奇怪的行为,

该wait.until方法,它的作用是调用该函数的应用方法,直到它返回的东西或者有超时。

据预计到有时会收到NotFoundExceptions如果元素尚未创建,实际上如果在$ C $看看c您会看到这一点:

 而(clock.isNowBefore(完)){
  尝试{
    t值= isTrue.apply(驱动器);    如果(值= NULL&放大器;!&安培; Boolean.class.equals(value.getClass())){
      如果(Boolean.TRUE.equals(值)){
        返回值;
      }
    }否则如果(值!= NULL){
      返回值;
    }
  }赶上(NotFoundException E){
    在许多情况//常见的情况,所以燕子在这里,但准备
    //如果它从未出现的元素重新抛出。
    lastException = E;
  }
  睡觉();

据捕获异常并执行与它无关,问题是,你覆盖了适用于捕捉和记录的异常,所以你看到的是预期的行为,有没有其他的方法来检查,如果该元素已创建,不断地要求它。

我自己,我用这个code函数发生器:

 公共静态功能< webdriver的,WebElement> presenceOfElementLocated(
        最终通过定位器){
    返回新的功能与LT; webdriver的,WebElement>(){
        @覆盖
        公共WebElement申请(webdriver的驱动程序){
            返回driver.findElement(定位);
        }
    };
}

时的简单了很多,你可以使用任何定位

I have been desperately trying to get WebDriver to click a button or link reliably, but it just does not want to cooperate. I have tried different methods from setting an implicit timeout, to the below code which is suppose to click and wait for the element to appear.

The below snippet of code was found somewhere on the internet, and it the closest I have come to reliably getting a button or link to click. Except it does not work the same in debug mode as it does when executed during my nightly regression testing.

Does anyone know of another method to click a button or link in a browser? Or should I be using Selenium 1 and not WebDriver as it is too new to be used reliably.

public void waitAndClick(WebDriver driver, By by) {
    WebDriverWait wait = new WebDriverWait(driver, 10000, 2000);
    Function<WebDriver, Boolean> waitForElement = new waitForElement(by);
    wait.until(waitForElement);

    Actions builder = new Actions(driver);
    builder.click(driver.findElement(by))
            .perform();
}

private class waitForElement implements Function<WebDriver, Boolean> {
    private final By by;

    private String text = null;

    public waitForElement(By by) {
        this.by = by;
    }

    public waitForElement(By by, String text) {
        this.by = by;
        this.text = text;
    }

    @Override
    public Boolean apply(WebDriver from) {
        if (this.text != null) {
            for (WebElement e : from.findElements(this.by)) {
                if (e.getText().equals(this.text)) {
                    return Boolean.TRUE;
                }
            }

            return Boolean.FALSE;
        } else {
            try {
                driver.switchTo().defaultContent().switchTo().frame("top");
                from.findElement(this.by);
            } catch (Exception e) {
                logger.error("Unable to find \"" + this.by.toString() + "\". Retrying....");
                return Boolean.FALSE;
            }
            logger.info("Found \"" + this.by.toString() + "\".");
            return Boolean.TRUE;
        }
    }
}

Console out in Eclipse Debug mode:

16:07:08,109 INFO  WebDriverUtility: apply Found "By.linkText: Classes".
16:07:10,514 INFO  WebDriverUtility: apply Found "By.linkText: Reports".
16:07:17,028 ERROR WebDriverUtility: apply Unable to find "By.linkText: Users". Retrying....
16:07:26,369 INFO  WebDriverUtility: apply Found "By.linkText: Users".
16:07:38,272 ERROR WebDriverUtility: apply Unable to find "By.linkText: System". Retrying....
16:07:41,334 INFO  WebDriverUtility: apply Found "By.linkText: System".
16:07:47,722 ERROR WebDriverUtility: apply Unable to find "By.linkText: Schools". Retrying....
16:07:50,565 INFO  WebDriverUtility: apply Found "By.linkText: Schools".

Console out when running from Eclipse:

16:14:04,179 INFO  WebDriverUtility: apply Found "By.linkText: Classes".
16:14:04,726 INFO  WebDriverUtility: apply Found "By.linkText: Reports".
16:14:09,771 INFO  PageAPITesting: login org.openqa.selenium.NoSuchElementException: Unable to find element with link text == Reports (WARNING: The server did not provide any stacktrace information)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.0rc3', revision: '12536', time: '2011-06-20 18:19:52'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_24'
Driver info: driver.version: RemoteWebDriver
16:14:09,865 INFO  PageAPITesting: login current tabs is Classes
16:14:09,958 INFO  WebDriverUtility: apply Found "By.linkText: Schools".
16:14:10,240 INFO  PageAPITesting: login java.lang.IllegalStateException: Unable to navigate to the ca.schoolspecialty.qa.api.pages.schools.MenuSchoolPage page

解决方案

I don't see any strange behavior,

The wait.until method, what it does is to call the apply method on the function until it returns something or there is a Time Out.

It's expected to receive NotFoundExceptions sometimes if the element hasn't been created yet, actually if you look in the code you'll see this:

 while (clock.isNowBefore(end)) {
  try {
    T value = isTrue.apply(driver);

    if (value != null && Boolean.class.equals(value.getClass())) {
      if (Boolean.TRUE.equals(value)) {
        return value;
      }
    } else if (value != null) {
      return value;
    }
  } catch (NotFoundException e) {
    // Common case in many conditions, so swallow here, but be ready to
    // rethrow if it the element never appears.
    lastException = e;
  }
  sleep();

It catches the exception and does nothing with it, the problem is that you overwrote the apply to catch and logs that exception, so what you are seeing is the expected behavior, there's no other way to check if the element has been created that continuously asking for it.

I myself I'm using this code for the Function generator:

public static Function<WebDriver, WebElement> presenceOfElementLocated(
        final By locator) {
    return new Function<WebDriver, WebElement>() {
        @Override
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    };
}

Is a lot simpler and you can use for any Locator

这篇关于webdriver的不可靠单击链接或按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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