테스트중인 애플리케이션에는 테이블을 계속해서 파싱해야합니다. 내가 현재 사용하고있는 방법은 매우 큰 테이블에 대한 느린, 그래서 내가 시도해 볼 수도 있습니다 생각을 멀티 스레드와 이러한 요소 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
미리 감사드립니다.
당신은 그렇게해서는 안된다. ['WebDriver'는 스레드로부터 안전하지 않습니다.] (https://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Is_WebDriver_thread-safe?) 그러면 이것은 예측할 수없는 미친 행동으로 이어질 것입니다. – acdcjunior
좋아요, 그래서 더 나은 접근법은 아마도 CSS 선택기가 테이블을 얻는 데 걸리는 시간을 줄이고 시도하는 것이겠습니까? driver.getPageSource를 저장하고 저장 한 다음 테이블 대신 구문 분석하고 어떻게 든 findElements (By.cssSelector());를 사용하여 페이지 소스를 구문 분석 할 수 있습니까? – bbarke
'느린'속도는 얼마나됩니까? 이 코드는 모두 사용중인 코드입니까, 느린 부분입니까? – Arran