2017-07-09 26 views
0

我正在嘗試使用Selenium來打開HTML頁面並單擊按鈕。 我回來的HTML是:Selenium Java |查找/單擊按鈕

<html> 
<head> 
    <link rel="shortcut icon" type="image/x-icon"> 
    <link rel="stylesheet" type="text/css" href="http://localhost:5050/style.css"> 
    <title>Test</title> 
</head> 
<body> 
    <div class="btn-container"> 
    <button class="btn-orange" id="successButton" name="Success" value="Success"> Success </button> 
    <button class="btn-orange" id="failButton" name="Fail" value="Fail"></button> Fail 
    </div> 
    </div> 
    <footer class="footer" align="center"> 
    <div class="container-fluid"> 
    <div class="clearfix"> 
    <div class="cards pull-left"> 
    </div> 
    </div> 
    </div> 
    </footer> 
</body> 
</html> 

我試圖點擊successButton,但它似乎沒有工作,我嘗試通過其ID訪問。

這裏是我用來click行:

driver.findElement(By.id("successButton")).click(); 

這是我的全部功能:

public void openTheHtmlPageAndClickButton(
           String pageUrl, 
           String SiteUrl, 
           String buttonValue) { 

     String lastUrl = null; 
     boolean timeout = true; 

     for (int tryNumber = 1; tryNumber <= 5 && timeout; tryNumber++) { 
      WebDriver driver = null; 
      try { 
       driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox()); 
       System.out.println("Selenium open: " + pageUrl); 
       driver.get(pageUrl); 
       int i = 0; 
       Alert alert; 

       while (i++ < 30) { 
        try { 
         alert = driver.switchTo().alert(); 
         if (alert != null) { 
          alert.accept(); 
         } 
        } catch (NoAlertPresentException e) { 

        } 
        String currentUrl = driver.getCurrentUrl(); 
        driver.findElement(By.id("successButton")).click(); 
        if (!currentUrl.equals(lastUrl)) { 
         System.out.println("currentUrl: " + currentUrl); 
         lastUrl = currentUrl; 
         if (currentUrl.startsWith(SiteUrl)) { 
          timeout = false; 
          break; 
         } 
        } else { 
         try { 
          Thread.sleep(5000); 
         } catch (InterruptedException e1) { 
          Assert.fail(); 
         } 
        } 

       } 
      } catch (Exception e) { 
       System.out.println("Selenium exception: " + e.toString()); 
      } finally { 
       if (driver == null) { 
        Assert.fail("Cannot open web driver, probably Selenium docker is down"); 
       } else { 
        if (timeout) { 
         System.out.println("Page got timeout: page source: " + driver.getPageSource()); 
         if (tryNumber == 5) { 
          Assert.fail("Page got timeout 3 times!!!"); 
         } 
        } 
        driver.quit(); 
       } 
      } 
     } 
    } 

請告知我在做什麼錯。

+1

該代碼看起來很好。調試並看到,該元素在'click()'時可能未加載的變化。所以使用'element.isDisplayed()'確保該元素可見。 –

+1

請仔細閱讀[問],特別是關於[mcve](MCVE)的部分,以及[預計需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort這是預期的堆棧溢出用戶)這將幫助您調試自己的程序併爲自己解決問題。如果你這樣做並且仍然卡住,你可以回過頭來發布你的MCVE,你試過的,以及執行結果,包括任何錯誤信息,這樣我們可以更好地幫助你。還提供了一個鏈接到頁面和/或相關的HTML。 – JeffC

+0

@JeffC,這不是我的第一個問題,但我希望提供儘可能多的細節,因爲這是我第一次使用硒。 –

回答

0

顯然,當我嘗試「點擊」它時,按鈕沒有出現。稍微改變了超時時間,問題就解決了。

相關問題