Java 等待页面元素(xpath)出现在SeleniumWebDriver中最有效的方法是什么?

Java 等待页面元素(xpath)出现在SeleniumWebDriver中最有效的方法是什么?,java,selenium,xpath,performance-testing,Java,Selenium,Xpath,Performance Testing,我使用Java和SeleniumWebDriver来测试单页web应用程序的功能 因此,很明显,元素是动态地从DOM中注入和移除的 我知道我可以使用与WebDriverWait类似的代码等待DOM中出现元素(我编写的非常简洁的模板略有变化): public void waitForElement()引发异常{ /* 在任何网页中插入以下代码段以测试该方法 按我 */ System.setProperty(“webdriver.gecko.driver”,“C:\\ProgramFiles(x86

我使用Java和SeleniumWebDriver来测试单页web应用程序的功能

因此,很明显,元素是动态地从DOM中注入和移除的

我知道我可以使用与WebDriverWait类似的代码等待DOM中出现元素(我编写的非常简洁的模板略有变化):

public void waitForElement()引发异常{
/*
在任何网页中插入以下代码段以测试该方法
按我
*/
System.setProperty(“webdriver.gecko.driver”,“C:\\ProgramFiles(x86)\\Mozilla Firefox\\geckodriver.exe”);
WebDriver=getDriver();
WebDriverWait wait=新的WebDriverWait(driver,10);//可以根据测试规范减少10
驱动程序。获取(“http://www.google.com");
WebElement response=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“/*[@class='i-am-your-class']));
响应。单击();
System.out.println(“*************************************************************************************************************************************”);
System.out.println(响应);
System.out.println(response.getText());
driver.close();
}
我想知道的是,这是否也是使用xpath获得此类结果的更有效方法

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


谢谢你的时间和帮助。

有几个事实需要考虑如下:

WebElement response = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='i-am-your-class']")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='i-am-your-class']"))).click();
  • WebDriverWait()。考虑按测试规格减少。例如,将其设置为10秒:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    
  • 在调用
    click()
    方法时继续前进,因此使用如下方法代替
    visibilityOfElementLocated()
    方法的预期条件:

    WebElement response = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='i-am-your-class']")));
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='i-am-your-class']"))).click();
    
  • 通过.xpath(“//标记名[@class='i-am-your-class'])”优化xpath,包括
    中的标记名。例如:

    By.xpath("//a[@class='i-am-your-class']")
    
  • 优化代码,以便在通过WebDriverWait返回元素后立即调用
    click()
    ,如下所示:

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

    • 我不知道您为什么说首选XPATH。我想补充一些关于定位器的内容

      在代码中编写的XPATH可以很容易地被
      css选择器替换。这是一个例子

      div[class='i-am-your-class']  
      
      现在主要的问题是为什么要切换到不同的定位器

      • 提高效率
      • 与xpath相比速度更快
      • 更可靠
      • 更多性能
      您应该始终按照硒贡献者建议的顺序使用定位器

    • ID
    • 名称
    • 类名
    • 链接文本
    • partialLinkText
    • 标记名
    • css选择器
    • XPath 注意:大多数情况下,cssSelector可以取代Xpath,但是Xpath有它自己的优势,cssSelector没有提供这些优势


      有关更多参考信息,您可以浏览此SO链接:

      有效方式的度量将在很大程度上取决于方法
      enterSearchTerm()
      您可能在哪里使用了xpath以及xpath是否优化。您的建议很有意义,@DebanjanB。我用一个更详细的例子编辑了答案。@Pitto:您必须使用xpath吗?或者您可以使用任何定位器?@cruisepandey XPath是首选,但我正在寻找最有效的方法来定位DOM中的新元素(由Angular/React/anything创建)。因此,如果您认为XPath不是最好的方法,我很乐意阅读您关于获得结果的最佳方法的想法。您的CSS选择器看起来更像XPath…;)。当您将任何定位器写为
      attribute='value'
      时,这是一个精确的字符串匹配。这意味着,如果类不完全且仅是
      i-am-your-class
      ,则它将不匹配。最好将其写成
      div.i-am-your-class
      。它不仅更短,更容易阅读,它将匹配任何包含该类的
      DIV
      ,但不限于该类,因为
      class='value'
      可能会匹配。@JeffC:是的,我当然会选它!;)谢谢,非常好的分享和思考的动力。你认为使用WebDriverWait是个好主意还是使用FluentWait更好?@Pitto WebDriverWait是FluentWait的定制版本。因此,请继续使用WebdriverWait,直到和除非您确实需要FluentWait抱歉打扰您,但。。。这个解决方案是更好还是相同?driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);driver.get(“);WebElement myDynamicElement=driver.findelelement(By.id(“i-am-your-id”);@Pitto作为初学者,您可以选择从
      implicitlyWait()
      thread.sleep()
      开始。粗略
      thread.sleep()
      会降低您的测试性能并隐式wait()可能很快就会死于火灾。继续前进,向WebDriverWait的优化解决方案过渡。请随意提问。您完全澄清了我的所有疑问,@DebanjanB!WebDriverWait是我想的真正的解决方案。真的谢谢。