TestNG 超时错误

Timeout error in TestNG

package ant;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;


public class NewTestNG {
public WebDriver driver;

@BeforeMethod
public void LAunchbrowser() {
  driver = new FirefoxDriver();
  driver.get("https://www.google.co.in/");
  //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

}
  @Test
  public void main() {
  Actions action = new Actions(driver);
  WebDriverWait wait = new WebDriverWait (driver, 20);
  WebElement w= 
   wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
  [@id='gs_htif0']")));
   WebElement a=   driver.findElement(By.xpath(".//*[@id='gs_htif0']"));    
   action.moveToElement(a).click().sendKeys("Shirt").build().perform(); 
  driver.findElement(By.xpath("//div[@value='Search']")).click();


 }}

出现以下错误:

FAILED: main org.openqa.selenium.TimeoutException: Timed out after 20 seconds waiting for element to be clickable: By.xpath: .//*[@id='gs_htif0'] Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:09' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 93e46eb2-2ba1-479b-9bfd-c56178d7eb7c at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:261) at ant.NewTestNG.main(NewTestNG.java:28)

我认为在您的 html 页面中找不到该元素,可能是您的 xpath 不正确。您可以尝试使用 chrome 开发人员工具 > 控制台选项卡检查它,然后键入 $x(".//[@id='gs_htif0']") 并查看它是否 returns 任何东西。也许你的 xpath 应该是 "//[@id='gs_htif0']"

用下面提到的代码替换您的@Test 方法代码。

    WebDriverWait wait = new WebDriverWait (driver, 20);
    WebElement w= 
            wait.until(ExpectedConditions.elementToBeClickable(By.id("lst-ib")));
    w.sendKeys("Shirt"); 
    driver.findElement(By.id("_fZl")).click();

让我知道它是否适合你。