Selenium WebDriver 是线程安全的吗?

5
更具体地说,同时在单个WebDriver/WebElement上执行多个操作是否安全?例如:像这样的操作。
WebDriver driver; //driver initialized somehow
final WebElement elem = driver.findElement(By.cssSelector("#elementID"));

//simplified for example, but in real code I'd be storing the results of these calls
new Thread() {
    @Override
    public void run() {
        elem.isDisplayed();
    }
}.run();
new Thread() {
    @Override
    public void run() {
        elem.isEnabled();
    }
}.run();

我曾经在本地交互时自己尝试过这个,没有遇到问题,但是在远程selenium网格上做同样的事情时会不时出现问题。
我不确定我遇到的问题是来自Selenium本身,还是Selenium没问题,而是我正在使用的托管网格提供程序的限制。Python中使用Selenium进行爬取时,Selenium是否线程安全?提到Selenium可能不是线程安全的,但我找不到任何确认的证据。
1个回答

13

这个问题在这里有答案。

"WebDriver不是线程安全的。话虽如此,如果您可以序列化对底层驱动程序实例的访问,那么可以将引用共享到多个线程中。这并不可取。相反,您可以为每个线程实例化一个WebDriver实例。"


链接已经失效,但是答案还是非常有帮助的。 - undetected Selenium
1
已更新失效链接 - https://github.com/SeleniumHQ/selenium/wiki/Frequently-Asked-Questions#q-is-webdriver-thread-safe - ramkumar-yoganathan
最好使用远程WebDriver,不是吗?https://github.com/SeleniumHQ/selenium/wiki/RemoteWebDriver - Fractal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接