如何在Selenium webdriver中打开chrome?

lvmkulzt  于 2022-12-16  发布在  Go
关注(0)|答案(5)|浏览(216)

我尝试用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浏览器打开但无法继续。以下错误的原因可能是什么:

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.
hc8w905p

hc8w905p1#

您没有正确启动驱动程序
webdriver.chrome.driver应该是您下载的驱动程序的路径,而不是Chrome的物理**位置。

v2g6jxz6

v2g6jxz62#

首先,您需要从这个链接下载chrome驱动程序文件,然后将其JAR导入eclipse中的包。
Download the link from here
然后,您必须将其导入到程序中。

import org.openqa.selenium.chrome.ChromeDriver;

然后创建一个驱动程序示例

driver = new ChromeDriver();

下载chrome的外部JAR
日 eclipse :右键单击相应的包(在包资源管理器中)并单击属性。2转到Java构建路径并添加外部jar。3现在添加chrome的jar文件。4然后按照我在ans中写的步骤导入chrome驱动程序并创建示例
按照照片中的步骤操作。1)

从这里选择你的文件并右键单击

wmomyfyw

wmomyfyw3#

下面的代码片段展示了如何使用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();
 
    }
u91tlkcl

u91tlkcl4#

使用最新版本的ChromeDriver。

资料来源|

http://chromedriver.storage.googleapis.com/index.html
lymnna71

lymnna715#

您需要先设置浏览器设置。如果有帮助,请尝试以下代码:

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();
}

您需要通过将鼠标悬停在错误行上来导入文件。

相关问题