【问题标题】:Selenium webdriver throwing timeout exceptionSelenium webdriver抛出超时异常
【发布时间】:2016-09-16 04:22:49
【问题描述】:

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

我做错了什么吗?

【问题讨论】:

  • 请先在浏览器控制台使用这个javascript document.getElementsByName('SearchButton') 确保你得到这个按钮?

标签: selenium selenium-webdriver timeoutexception


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您应该使用By.id 而不是By.name。因此,请使用以下任何一种:

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

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

    【讨论】:

      【解决方案3】:
          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;
          }
          }
      

      【讨论】:

      • 知道了。元素包含在表格中是否有任何不同的方式来访问包装在 td 中的元素下面是 HTML 代码i.stack.imgur.com/WxJYp.jpg
      • 专家的任何回答
      猜你喜欢
      • 2013-02-22
      • 2018-07-14
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2017-11-25
      相关资源
      最近更新 更多