Selenium Webdriver 抛出超时异常

问题描述 投票:0回答:3

我是

Selenium
的新手。

我的问题是,我正在尝试单击一个元素,但

Selenium
抛出
timeout exception
,即使我增加了超时值。

我需要使用

xpath
代替
id
吗?

HTML 代码是:

我的代码看起来像这样

 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");
       }

我做错了什么吗?

selenium selenium-webdriver timeoutexception
3个回答
0
投票

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


0
投票

您应该使用

By.name
,而不是
By.id
。因此,请使用以下任一方法:

  1. By.Id("SearchButton")
  2. By.CssSelector("input#SearchButton")
  3. By.Xpath("//input[@id='SearchButton']")

注意:语法可能有错误,请根据您的编程语言进行调整


-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;
    }
    }
© www.soinside.com 2019 - 2024. All rights reserved.