2013-06-20 6 views
1

테스트중인 애플리케이션에는 테이블을 계속해서 파싱해야합니다. 내가 현재 사용하고있는 방법은 매우 큰 테이블에 대한 느린, 그래서 내가 시도해 볼 수도 있습니다 생각을 멀티 스레드와 이러한 요소 simutaniuslyWebDriver 인스턴스에서 한 페이지에 여러 요소를 동시에 가져올 수 있습니까?

public class TableThread 
    implements Runnable 
{ 

    private WebDriver driver; 
    private String thread; 

    TableThread(WebDriver driver, String thread) 
    { 
    this.driver = driver; 
    this.thread = thread; 
    } 

    @Override 
    public void run() 
    { 
    getTableRow(); 
    } 

    private String getTableRow() 
    { 
    System.out.println("start getting elements " + thread); 

    WebElement tableElement = driver.findElement(By.className("logo")); 
    String href = tableElement.getAttribute("href"); 
    System.out.println("Table Att: " + thread + " " + href); 

    return href; 

    } 

} 

및 호출 루프를 얻는 방법

for (int i = 0; i < 10; i++) { 

    ExecutorService threadExecutor = Executors.newSingleThreadExecutor(); 
    TableThread tableThread = new TableThread(driver, "Thread " + i); 
    threadExecutor.execute(tableThread); 
    Thread.sleep(50); 
} 

오류가 설정시 this.driver = ThreadGuard.protect (driver);

start getting elements 
Exception in thread "pool-1-thread-1" org.openqa.selenium.WebDriverException: Thread safety error; this instance of WebDriver was constructed on thread main (id 1) and is being accessed by thread pool-1-thread-1 (id 26)This is not permitted and *will* cause undefined behaviour` 

this.driver = driver;

Exception in thread "pool-2-thread-1" org.openqa.selenium.UnsupportedCommandException: Error 404: Not Found 
Exception in thread "pool-1-thread-1" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to java.lang.String 

미리 감사드립니다.

+0

당신은 그렇게해서는 안된다. ['WebDriver'는 스레드로부터 안전하지 않습니다.] (https://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Is_WebDriver_thread-safe?) 그러면 이것은 예측할 수없는 미친 행동으로 이어질 것입니다. – acdcjunior

+0

좋아요, 그래서 더 나은 접근법은 아마도 CSS 선택기가 테이블을 얻는 데 걸리는 시간을 줄이고 시도하는 것이겠습니까? driver.getPageSource를 저장하고 저장 한 다음 테이블 대신 구문 분석하고 어떻게 든 findElements (By.cssSelector());를 사용하여 페이지 소스를 구문 분석 할 수 있습니까? – bbarke

+0

'느린'속도는 얼마나됩니까? 이 코드는 모두 사용중인 코드입니까, 느린 부분입니까? – Arran

답변

0

WebDriver 클래스가 스레드로부터 안전하지 않다는 것을 @ acdcjunior의 설명에 추가하기 만하면됩니다. 나는이 질문과 관련된 것으로 생각되는 다음과 같은 문제점을 발견했다 : Issue 6592: IE Driver returns 404: File not found

동일한 문제인 경우, WebDriver와 통화를 동기화하는 것이 하나의 해결책이다. 필자는 기본 웹 드라이버 호출을 동기화하는 SynchronizedInternetExplorerDriver.java 클래스를 만들어이 문제를 해결했습니다.

이것이 도움이되는지 알려 주시면 (이 SynchronizedInternetExplorerDriver를 사용하면 스레드 문제를 원인으로 제외 할 수 있습니다).

코드 조각 :

public class SynchronizedInternetExplorerDriver extends InternetExplorerDriver { 

... 

    @Override 
    protected Response execute(String driverCommand, Map<String, ?> parameters) { 
     synchronized (getLock()) { 
      return super.execute(driverCommand, parameters); 
     } 
    } 

    @Override 
    protected Response execute(String command) { 
     synchronized (getLock()) { 
      return super.execute(command); 
     } 
    } 

... 

} 

전체 코드 :SynchronizedInternetExplorerDriver.java

관련 문제