Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以从WebDriver实例同时获取页面上的多个元素?_Java_Selenium - Fatal编程技术网

Java 是否可以从WebDriver实例同时获取页面上的多个元素?

Java 是否可以从WebDriver实例同时获取页面上的多个元素?,java,selenium,Java,Selenium,我正在测试的应用程序有大量需要不断解析的表。我目前使用的方法对于非常大的表来说速度很慢,所以我想我可以尝试使用多线程方法,同时获得这些元素 public class TableThread implements Runnable { private WebDriver driver; private String thread; TableThread(WebDriver driver, String thread) { this.driver = driver;

我正在测试的应用程序有大量需要不断解析的表。我目前使用的方法对于非常大的表来说速度很慢,所以我想我可以尝试使用多线程方法,同时获得这些元素

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=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

提前谢谢

只是在@acdcjunior的评论中添加了WebDriver类不是线程安全的;我发现了以下问题,我认为与这个问题有关:

如果这与您遇到的问题相同,那么一个解决方案就是通过WebDriver同步您的呼叫。我通过创建一个
SynchronizedInternetExplorerDriver.java
类来解决这个问题,该类同步对底层WebDriver的调用

让我知道这是否有帮助(使用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);
        }
    }

...

}
公共类同步InternetExplorerDriver扩展InternetExplorerDriver{
...
@凌驾
受保护的响应执行(字符串驱动器命令、映射参数){
已同步(getLock()){
返回super.execute(driverCommand,参数);
}
}
@凌驾
受保护的响应执行(字符串命令){
已同步(getLock()){
返回super.execute(命令);
}
}
...
}

完整代码:

您不应该这样做。所以这肯定会导致疯狂的不可预测行为。好吧,那么更好的方法可能是尝试减少css选择器获取表格所需的时间?是否可以将driver.getPageSource保存下来,然后为表解析它,并以某种方式使用
findelelements(By.cssSelector())以这种方式解析页面源代码?慢到什么程度?这就是您正在使用的全部代码,其中哪一部分比较慢?要获得一个包含2列和5行的表大约需要9秒钟。我使用的解决方案与我们发现的同样值得注意的解决方案类似,我不仅得到了表格文本,还得到了表格中的一些值,如类型、类和名称。我认为给我们一个示例表格和您使用的代码可以更有效地找到一个好的解决方案。
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);
        }
    }

...

}