[英]How to open chrome in Selenium webdriver?
I'm trying to launch chrome with Selenium Webdriver
and used the following code:我正在尝试使用
Selenium Webdriver
启动 chrome 并使用以下代码:
System.setProperty("webdriver.chrome.driver",
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.yahoo.com");
Chrome browser opens but is not proceeding further. Chrome 浏览器打开但没有继续。 What could be the reason for the following error:
以下错误的原因可能是什么:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
You are incorrectly starting up the driver 您错误地启动了驱动程序
webdriver.chrome.driver
is supposed to be the path to the driver that you've downloaded and not Chrome's physical location. webdriver.chrome.driver
应该是您下载的驱动程序的路径,而不是 Chrome的物理位置。
First you need to download chrome driver file from this link and than import its JAR to the package in eclipse. 首先,您需要从此链接下载chrome驱动程序文件,然后将其JAR导入Eclipse中的包中。
Download the link from here 从这里下载链接
Then you will have to import it in your program. 然后,您将必须将其导入程序中。
import org.openqa.selenium.chrome.ChromeDriver;
and than make a driver instance 并且比做一个驱动程序实例
driver = new ChromeDriver();
Download the external JAR of chrome 下载Chrome的外部JAR
In eclipse :: right click on the respective package(in the package explorer) and click on the properties. 在eclipse ::中,右键单击相应的包(在包资源管理器中),然后单击属性。 Go to Java build path and add external jars.
转到Java构建路径并添加外部jar。 Now add the jar file of chrome .
现在添加chrome的jar文件。 and than follow the steps i wrote in the ans which was to import the chrome driver and creating an instance
然后按照我在ans中编写的步骤操作,即要导入chrome驱动程序并创建实例
Follow these steps in the photograph. 请按照照片中的这些步骤进行操作。 1)
1)
select your file from here and right click 从此处选择文件,然后单击鼠标右键
Below snippet shows how you can open chrome browser using selenium webdriver.下面的代码片段显示了如何使用 selenium webdriver 打开 chrome 浏览器。
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting the webdriver.chrome.driver property to its executable's location
System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe");
//Instantiating driver object
driver = new ChromeDriver();
//Using get() method to open a webpage
driver.get("https://stackoverflow.com");
//Closing the browser
driver.quit();
}
Use the latest versions of ChromeDriver. 使用最新版本的ChromeDriver。
Source| 来源|
http://chromedriver.storage.googleapis.com/index.html
You need to setup your browser settings first. 您需要先设置浏览器设置。 Try below-mentioned code if it helps:
如果有帮助,请尝试下面提到的代码:
public void setup ()
{
System.setProperty("webdriver.chrome.driver", "C:\\**PATH**\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();
}
You'll need to import files by hovering on error lines. 您需要通过将鼠标悬停在错误行上来导入文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.