【问题标题】:selenium headless test headless modeselenium 无头测试无头模式
【发布时间】:2018-05-29 05:01:34
【问题描述】:

我正在使用 chromedriver 运行 selenium 测试。但现在我需要在无头模式下做同样的事情。

以下是无头模式运行良好的程序--

WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com");                    
WebElement element = driver.findElement(By.name("q"));  
element.sendKeys("Guru99"); 
element.submit();           
System.out.println("Page title is: " + driver.getTitle());      
driver.quit();

现在问题来了—— 当我尝试使用 idxpath 读取元素时,程序以异常结束。

错误是

没有找到这样的元素。

如何纠正这个错误。我们欢迎任何帮助。

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及重现它所需的最短代码在问题本身。没有明确的问题陈述的问题对其他读者没有用处。见:How to create a Minimal, Complete, and Verifiable example.
  • 尝试在元素之前添加显式等待。
  • @RamanGupta idxpath 看起来像你在 program 中尝试的那样,当我尝试时以异常结束用它的 id 或 xpath 读取元素?
  • ID - lst-ib, XPATH - //*[@id="lst-ib"]

标签: java selenium


【解决方案1】:

导入这些:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

并使用它:

WebDriverWait wait5s = new WebDriverWait(driver,5);

WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com");

WebElement element = wait5s.until(ExpectedConditions.elementToBeClickable(By.name("q")));  

element.sendKeys("Guru99"); 
element.submit();           
System.out.println("Page title is: " + driver.getTitle());      
driver.quit();

您应该能够通过 ID、xpath 等找到元素。

【讨论】:

    猜你喜欢
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    相关资源
    最近更新 更多