java 如何在 selenium webdriver 中将 Web 浏览器从 Firefox 更改为 Chrome/Opera/IE/Safari?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11920353/
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-10-31 06:56:47  来源:igfitidea点击:

How to change web browser from Firefox to Chrome/Opera/IE/Safari in selenium webdriver?

javaseleniumselenium-webdriverselenium-chromedriver

提问by pranky301

How to change browser from firefox to Chrome/Opera/IE working in selenium webdriver? Please guide in steps and also with the code snippet.

如何在 selenium webdriver 中将浏览器从 firefox 更改为 Chrome/Opera/IE?请按步骤进行指导,并附上代码片段。

Please reply if you have answer for any of the browsers mentioned above.

如果您对上述任何浏览器有答案,请回复。

I read out a lot on this but could not link it properly.

我对此读了很多,但无法正确链接。

回答by some_other_guy

First of all you need to import the proper drivers into the project/class.

首先,您需要将正确的驱动程序导入到项目/类中。

like

喜欢

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

etc.

等等。

The you need to create new Webdrivers for the broswer you need.

您需要为所需的浏览器创建新的 Webdriver。

like

喜欢

WebDriver driver = new FirefoxDriver();
WebDriver driver = new InternetExplorerDriver();
WebDriver driver = new ChromerDriver();

etc for each browser.

等每个浏览器。

NOTE:It is tough to use different browsers/drivers in a single test. Either you may use similar tests for each browser and maintain a test-suite (i.e. use one driver and import in a test and maintain similar test for other browsers) or you can use some config files or excel to pick up which browser you want the test to run. You might like to explore http://htmlunit.sourceforge.net/for headless testing.

注意:在一次测试中很难使用不同的浏览器/驱动程序。您可以为每个浏览器使用类似的测试并维护一个测试套件(即使用一个驱动程序并在测试中导入并为其他浏览器维护类似的测试),或者您可以使用一些配置文件或 excel 来选择您想要的浏览器测试运行。您可能想探索 http://htmlunit.sourceforge.net/进行无头测试。

And information about OperaDriver can be found here: - https://github.com/operasoftware/operadriver/

关于 OperaDriver 的信息可以在这里找到: - https://github.com/operasoftware/operadriver/