等待页面元素(xpath)在Selenium Webdriver中显示的最有效方法是什么? [英] What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver?

查看:283
本文介绍了等待页面元素(xpath)在Selenium Webdriver中显示的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java和Selenium Webdriver来测试单页Web应用程序的功能。

I am using Java and Selenium Webdriver in order to test the functionalities of a single page web application.

因此,很明显,元素被注入和删除动态地使用DOM。

For this reason, clearly, elements are injected and removed from the DOM dynamically.

我知道我可以使用类似的代码使用WebDriverWait等待元素存在于DOM中(我编写的非常简洁的模板稍微改变了 GitHub ):

I know I can wait for an element to be present in the DOM using similar code that is using WebDriverWait (very neat template I wrote slightly changing GitHub):

public void waitForElement() throws Exception {
    /*
    Inject the following snippet in any web page to test the method
    <button class="i-am-your-class" onclick="alert('Wow, you pressed the button!');">Press me</button>
     */
    System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
    WebDriver driver = getDriver();
    WebDriverWait wait = new WebDriverWait(driver, 10); // 10 can be reduced as per Test Specifications
    driver.get("http://www.google.com");
    WebElement response = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='i-am-your-class']")));
    response.click();
    System.out.println("*****************************************************************************");
    System.out.println(response);
    System.out.println(response.getText());

    driver.close();
}

我想知道的是,这也是更有效的方式使用xpath获得这样的结果。

What I would like to know is if this is also the more efficient way to obtain such result using an xpath.

我一直在研究Stackoverflow,有几个答案指向类似的方向,但没有答案专注于效率/性能:

I have been researching on Stackoverflow and several answers point in a similar direction but no answer is focused on efficiency / performances:

  • WebDriver - wait for element using Java
  • Selenium WebDriver: wait for element to be present when locating with WebDriver.findElement is impossible

感谢您的时间和帮助。

推荐答案

您需要考虑以下几个事实:如下:

There are a couple of facts which you need to consider as follows :


  • WebDriverWait() 100 不应该是一名实时服务员。请考虑根据测试规范减少它。例如,将其设置为10秒:

  • WebDriverWait() for 100 shouldn't be a real-time waiter. Consider reducing it as per the Test Specifications. As an example, set it as 10 seconds :

WebDriverWait wait = new WebDriverWait(driver, 10);


  • 在调用时向前移动click()方法而不是 ExpectedConditions 而不是 visibilityOfElementLocated()方法使用 elementToBeClickable () 方法如下:

  • Moving forward as you are invoking click() method so instead of ExpectedConditions as visibilityOfElementLocated() method use elementToBeClickable() method as follows :

    WebElement response = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='i-am-your-class']")));
    


  • 优化 xpath ,包括 tagName By.xpath(// tagName [@ class ='i-am-your-class'])。例如:

    By.xpath("//a[@class='i-am-your-class']")
    


  • 优化代码以调用 click()一旦元素通过 WebDriverWait 返回,如下所示:

  • Optimize your code to invoke click() as soon as the element is returned through WebDriverWait as follows :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='i-am-your-class']"))).click();
    


  • 这篇关于等待页面元素(xpath)在Selenium Webdriver中显示的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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