2017-07-09 6 views
0

私はHTMLページを開いてボタンをクリックするためにSeleniumを使用しようとしています。私は戻って取得 HTMLは次のとおりです。セレン・ジャワ|問題を発見/クリックするボタン

<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

コードはよく見えます.Debugとsee、 'click()'の時に要素が読み込まれないかもしれないという変更があります。要素が見えるように 'element.isDisplayed()'を使ってください。 –

+1

[ask]、特に[mcve](MCVE)の部分、[どのくらいの研究努力が必要ですか](https://meta.stackoverflow.com/questions/261592/how-much-research-effort)をお読みください。 -is-expected-of-stack-overflow-users)これはあなた自身のプログラムをデバッグし、自分で問題を解決するのに役立ちます。あなたがこれを行い、まだまだ立ち往生している場合は、あなたがMCVEを投稿し、何を試したのか、そしてエラーメッセージを含む実行結果を投稿して、より良くあなたを助けることができます。また、ページや関連するHTMLへのリンクを提供してください。 – JeffC

+0

@JeffC、これは私の最初の質問ではありませんが、私はセレンを使って作業するので、できるだけ詳細を提供したいと思っていました。 –

答えて

0

「クリック」したときにボタンが表示されないようです。タイムアウトをわずかに変更し、問題は解決しました。

関連する問題