2017-09-29 8 views
-1

私は、Javaを使用してセレンコードを書いていると機能は以下のとおりです。効率的な方法は、セレンを使用してデータを取得する

  1. は、キーワードpolicesと検索を入力し、検索ボックスがあり、ブラウザを開いて、そのURLで指定したURL
  2. に移動します。
  3. ページあたり10個のポリシーがリストされ、合計125ページあります。

して、リンク上

  1. クリックします。
  2. リンクからデータを取得します。
  3. 前のページに移動します。
  4. 次をクリックします。
  5. 時にはそれが20ページまで検索し、時には7ページまで、そのページのすべてのポリシーの設定が完了した場合、[次へ]をクリックし、現在の問題がある

ステップ1から開始します。終わりまで行く方法はありません。その間に以下のようなエラーがスローされます。

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element ... is not clickable at point (358, 214). Other element would receive the click: (Session info: chrome=61.0.3163.100) (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:42:44.417Z' System info: host: 'HDC3-L-6441MKV', ip: '10.196.224.96', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=XP, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a), userDataDir=C:/Users/rakesh.keerthi/AppData/Local/Google/Chrome/User Data}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}] Session ID: 72dc7f70f0d15b842c217058582a10ec at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167) at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40) at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82) at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:646) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:275) at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:82) at OpenGoogleInFF.printResult(OpenGoogleInFF.java:48) at OpenGoogleInFF.main(OpenGoogleInFF.java:36)

ここに私のコードです。

import java.util.Date; 
import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.chrome.ChromeOptions; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class OpenGoogleInFF { 

    public static void main(String[] args) throws Exception { 
     System.setProperty("webdriver.chrome.driver", 
       "C:\\Users\\myUserName\\Downloads\\Selenium\\chromedriver.exe"); 
     String user = System.getProperty("user.name"); 
     ChromeOptions options = new ChromeOptions(); 
     options.addArguments("--user-data-dir=C:/Users/" + user + "/AppData/Local/Google/Chrome/User Data"); 
     options.addArguments("--start-maximized"); 
     WebDriver driver = new ChromeDriver(options); 

     driver.get("myUrl"); 
     Thread.sleep(6000L); 
     driver.findElement(By.id("SearchText")).sendKeys("policies"); 
     driver.findElement(By.className("ion-ios-search")).click(); 
     Thread.sleep(5000L); 
     int noOfPages = driver.findElements(By.xpath(".//*[@class='pagination']/li")).size() - 5; 
     System.out.println("no of pages are " + noOfPages); 
     System.out.println(new Date()); 
     for (int i = 0; i < noOfPages; i++) { 
      Thread.sleep(3000L); 
      List<WebElement> numberOfChildren = driver.findElements(By.xpath("//div[@ng-repeat='result in results']")); 
      int count = numberOfChildren.size(); 
      printResult(driver, count); 
      driver.findElement(By.xpath(".//*[@id='pagingControl']/ul/li[128]/a")).click(); 
     } 
     System.out.println(new Date()); 
    } 

    private static void printResult(WebDriver driver, int count) throws Exception { 
     for (int i = 1; i <= count; i++) { 
      WebElement element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOfElementLocated(
        By.xpath("html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[" + i 
          + "]/ul/span[2]/div/li[1]/h3/a"))); 

      scrollToElementByOffset(driver, element, -200).click(); 
      Thread.sleep(3000L); 

      (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
        By.xpath("html/body/div[1]/div/div[2]/div/div/section/section/div/div[4]/a[2]"))).click(); 
      String policyNumber = driver 
        .findElement(By 
          .xpath("html/body/div[1]/div/div[2]/div/div/section/section/div/div[6]/div[2]/table/tbody/tr[1]/td[2]")) 
        .getText(); 

      String policyStatement = driver.findElement(By.xpath(".//*[@id='policy-view']/section/div/div[3]/h1")) 
        .getText(); 

      String policyDetails = driver 
        .findElement(By 
          .xpath("html/body/div[1]/div/div[2]/div/div/section/section/div/div[8]/div/div/div[2]/div[2]/div")) 
        .getText(); 

      System.out.println(policyNumber + "\t" + policyStatement); 
      System.out.println("\n\n" + policyDetails + "\n\n"); 

      driver.navigate().back(); 
      driver.navigate().back(); 

     } 
    } 

    private static WebElement scrollToElementByOffset(WebDriver driver, WebElement element, int offset) { 
     JavascriptExecutor jse = (JavascriptExecutor) driver; 
     jse.executeScript("window.scrollTo(" + element.getLocation().getX() + "," 
       + (element.getLocation().getY() + offset) + ");"); 

     return element; 
    } 
} 

実際のURLを置き換えることについてお詫び申し上げます。これは私たちの組織の社内サイトです。

すべてのページの結果を得るためにコードを最適化/改善する方法と、合計実行時間を削減する方法を教えてください。私はクリックして行く次のページを時にはネットワークまたはブラウザの問題は、私がtrycatchで、私はそれを扱うproblem.Thenの同じ種類に直面したとき

おかげ

+0

[デバッグ中の要素がポイントでクリックできない]エラー(https://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error) –

+0

@Rotemben、this言及された質問の複製ではありません。私はこれを行う方法を知っている、ちょうど良い方法が必要だった。 – user3872094

+0

あなたのコードが動作しているので、これはあまり適していません。この問題を削除して、コードなどの改善を求めるときにcodereview.stackexchange.comに移動する必要があります。 – JeffC

答えて

0

数日前、私は似たタイプのプロジェクトのように動作します..すべてのエラーをスローした場合。私はちょうどあなたがあなたの問題を解決することができますこの方法を使用する。..

    int counts=0; 

        try { 
     //Here I write a code and execute 

         //Program END 
         counts=0; //if my program end then I consider all the operation successfully and assign value count=0 


       } 


       catch (org.openqa.selenium.WebDriverException e){ 

        count++; 
        System.out.println(count); 
         if(count>=7){ 
       //here if sequencially 7 time i get same error then i terminate current loop and send to next loop 
         break; 
         } 

同じエラーが、私はそれが実行されますそれ以外のループを終了時点で7を発生program.Ifを終了していないメッセージでそれを扱います..それがあなたを助けることを願って..

関連する問題