2017-04-17 83 views
0
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(); 


}} 

得到如下錯誤:超時錯誤TestNG中

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)

+0

您可以進行一些小的更改並更新我的狀態,如下所示:1.刪除TestNG環境設置並嘗試作爲純Java程序執行? 2.在作爲TestNG Test/Suite執行Test with Test註釋時,您不得將方法名稱用作「main」,將其更改爲其他內容並將我的狀態更新。 – DebanjanB

回答

0

用下面提到的代碼替換你的@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(); 

讓我知道它是否適合你。

+0

它的工作。感謝Akarsh.I猜xpatg是不正確的,但我直接從螢火蟲拿起。 – Tester

+0

你寫的Xpath很好,但是你使用的id有一個屬性aria-hidden =「true」,可能是這個id被隱藏了。我不確定。 – Akarsh

0

我覺得元素不能在HTML頁面中找到,也許你的XPath是不正確。您可以嘗試使用chrome開發人員工具>控制檯選項卡檢查它,然後鍵入$ x(「。// [@ id ='gs_htif0']」)並查看它是否返回任何內容。也許你的xpath應該是「// [@ id ='gs_htif0']」