Manish B
Manish B

Reputation: 429

Selenium webdriver throwing timeout exception

I am new to Selenium.

My issue is that I'm trying to click an element but Selenium is throwing a timeout exception, even if I increase the timeout value.

Do I need to use xpath instead of id?

The HTML Code is:

enter image description here My code looks like this

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

Am I doing anything wrong?

Upvotes: 0

Views: 8929

Answers (3)

kurakura88
kurakura88

Reputation: 2305

Instead of By.name, you should use By.id instead. Therefore, use either of these:

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

Note: syntax could be wrong, please adjust depending on your programming language

Upvotes: 0

Manjunatha.N
Manjunatha.N

Reputation: 45

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

Upvotes: -1

Anand
Anand

Reputation: 1939

The input type here is submit (by looking at your HTML code) so I would strongly advise to try the submit() function of Selenium.

Upvotes: 0

Related Questions