2016-05-19 121 views
0

我是Selenium的新手。硒webdriver抛出超时异常

我的问题是,我试图点击一个元素,但Selenium抛出一个timeout exception,即使我增加超时值。

我需要使用xpath而不是id吗?

的HTML代码是:

enter image description here 我的代码看起来像这样

void searchquotation() throws TimeoutException { 
    try { 
      WebDriverWait wait = new WebDriverWait(driver, 15); 
      WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("SearchButton"))); 
      element.click(); 
     } 
    catch(TimeoutException e) { 
     System.out.println("Timeout occured"); 
     } 

难道我做错了什么?

+0

请先确保在使用这个JavaScript document.getElementsByName(浏览器控制台‘搜索按钮’您收到此按钮)或不? –

回答

0

这里的输入类型是提交的(通过查看您的HTML代码),所以我强烈建议尝试Selenium的submit()函数。

-1
try below code, even timeout exception occurs, it will try 4 time to click on it. assuming locator is correct By.name("SearchButton") 

    public void searchquotation() 
    int count=0; 
    while(count<4) 
    { 
    try { 
    WebElement x = driver.findElement(By.name("SearchButton"))); 
    WebDriverWait element=new WebDriverWait(driver,15); 
    element.until (ExpectedConditions.presenceOfElementLocated(By.name("SearchButton"))); 
    x.click(); 
count=count+4; 
    } 
    catch(TimeoutException e) { 
    count=count+1; 
    System.out.println("Timeout occured"); 
    continue; 
    } 
    } 
+0

明白了。该元素是封闭在一个表是否有任何不同的方式来访问一个元素包装在td下面是HTML代码 http://i.stack.imgur.com/WxJYp.jpg –

+0

任何专家的答案 –

0

代替by.name,您应该使用by.id来代替。因此,使用以下两种:

  1. by.Id( “搜索按钮”)
  2. by.CssSelector( “输入#搜索按钮”)
  3. by.Xpath(“//输入[@ ID =”搜索按钮']“)

注:语法可能是错误的,请调整取决于你的编程语言