Selenium webdriver抛出超时异常

时间:2016-05-19 10:34:21

标签: selenium selenium-webdriver timeoutexception

我是Selenium的新手。

我的问题是我正在尝试点击一个元素,但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");
       }

我做错了吗?

3 个答案:

答案 0 :(得分:0)

此处的输入类型是提交(通过查看您的HTML代码),因此我强烈建议您尝试使用Selenium的submit()函数。

答案 1 :(得分:0)

而不是By.name,您应该使用By.id代替By.Id("SearchButton")。因此,请使用以下任一方法:

  1. By.CssSelector("input#SearchButton")
  2. By.Xpath("//input[@id='SearchButton']")
  3. {{1}}
  4. 注意:语法可能有误,请根据您的编程语言进行调整

答案 2 :(得分:-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;
    }
    }