multithreading Selenium 可以在一个浏览器中使用多线程吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30808606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:40:03  来源:igfitidea点击:

Can Selenium use multi threading in one browser?

multithreadingseleniumselenium-grid2

提问by Roy

I want to test a web in multi threading but when I open too many chromedrivers they use too much memory. Can I use multi threading in one browser?

我想在多线程中测试网络,但是当我打开太多 chromedrivers 时,它们使用了太多内存。我可以在一个浏览器中使用多线程吗?

回答by Vicky

WebDriver is not thread-safe.The issue of thread-safety isn't in your code but in the actual browser bindings. They all assume there will only be one command at a time (e.g. like a real user).But you can on the other hand instantiate one WebDriver instance for each thread but it will launch multiple browsers which will consume more memory...

WebDriver 不是线程安全的线程安全的问题不在您的代码中,而在实际的浏览器绑定中。他们都假设一次只有一个命令(例如,就像一个真正的用户一样)。但另一方面,您可以为每个线程实例化一个 WebDriver 实例,但它会启动多个浏览器,这将消耗更多内存......

Hope this helps you....Kindly get back if you have any queries

希望这对您有所帮助....如果您有任何疑问,请回复

回答by Manu

The multithreading should be done on different instances of Webdriver as Webdriver itself is a single thread.

多线程应该在 Webdriver 的不同实例上完成,因为 Webdriver 本身是一个单线程。

Different threads can be run on same Webdriver, but then the results of the tests would not be what you expected. Let me explain it.

不同的线程可以在同一个 Webdriver 上运行,但是测试的结果不会是你所期望的。让我解释一下。

When you use multithreading to run different tests on different tabs(that't not impossible, little bit of coding is required), the actions you will perform like click or send keys will go to the opened tab that is currently focused regardless of the test running. That means all the test will run simultaneously on the same tab that has focus and not on intended tab.

当您使用多线程在不同的选项卡上运行不同的测试时(这并非不可能,需要一点编码),您将执行的操作(例如单击或发送键)将转到当前关注的已打开选项卡,而不管测试如何跑步。这意味着所有测试将在具有焦点的同一个选项卡上同时运行,而不是在预期的选项卡上。

You can read about multithreading in Webdriver.

您可以在 Webdriver 中阅读有关多线程的信息