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:等待元素可点击20秒后超时:By.xpath:.//* [@ id ='gt_htif0']构建信息:版本:'2.53.1' ,修订版:'a36b8b1',时间:'2016-06-30 17:37:09'驱动信息:org.openqa.selenium.firefox.FirefoxDriver Capabilities [{applicationCacheEnabled = true,rotating = 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}]会话ID:93e46eb2-2ba1-位于org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:261)的org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)的479b-9bfd-c56178d7eb7c NewTestNG.main(NewTestNG.java:28)
2 回答
我认为你的html页面中找不到该元素,也许你的xpath不正确 . 您可以尝试使用chrome developer tools> console选项卡进行检查,然后输入$ x(“ . // [@ id = 'gs_htif0'] ") and see whether it returns anything. Maybe your xpath should be " // [@ id = 'gs_htif0']”
用下面提到的代码替换你的@Test方法代码 .
请让我知道这对你有没有用 .