简体   繁体   English

如何在 Selenium webdriver 中打开 chrome?

[英]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.

相关问题 如何使用 JAVA 在 Selenium WebDriver 中打开 Chrome 开发者控制台 - How to open Chrome Developer console in Selenium WebDriver using JAVA 如何使用 Selenium WebDriver 在新标签页(chrome)中打开链接? - How to open a link in new tab (chrome) using Selenium WebDriver? 如何使用Selenium Webdriver打开新的Chrome标签页? - How to open a new chrome tab using selenium webdriver? 如何使用Selenium WebDriver和Cucumber在Chrome中正确打开本地文件? - How to open correctly a local file within Chrome using Selenium WebDriver and Cucumber? 如何使用 Selenium webdriver 在移动模式下打开 Chrome 浏览器以在 JMeter 中进行负载测试? - How can I open the Chrome browser in mobile mode using the Selenium webdriver for load testing in JMeter? 如何在前台打开chrome驱动程序(使用Selenium Webdriver)。 默认情况下,它在背景中打开,没有焦点 - How to open chrome driver (Using Selenium Webdriver) in foreground. By default its opening in background with no focus 如何使用Selenium WebDriver在Chrome中激活AdBlocker? - How to Activate AdBlocker in Chrome using Selenium WebDriver? Selenium chrome webDriver - 关注最新打开的新标签java - Selenium chrome webDriver - get focus on the latest open new tab java 硒webdriver触发chrome,但无法打开获取URL - selenium webdriver trigger chrome but doesn't open the get url 如何使用 Selenium webdriver 打开特定浏览器 - How to open specific browser using Selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM