使用Selenium java和windows os

时间:2017-12-10 09:46:52

标签: java google-chrome selenium headless

使用Selenium java和Windows操作系统在chrome headless上运行脚本时,我遇到了以下问题。 URL没有打开我得到null作为我的应用程序URL的页面标题..Chrome驱动程序版本2.33,chrome浏览器62 ..我正在使用下面的代码

System.setProperty("webdriver.chrome.driver", chromedriver.exe);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("window-sized1200,600");
ChromeDriver driver = new ChromeDriver(chromeOptions);
driver.get("app url");
System.out.println(driver.getTitle)

是不是因为app URL不支持无头模式..没有任何异常..

2 个答案:

答案 0 :(得分:1)

你的窗口大小参数中有一个拼写错误,你打电话给addArguments,但每次调用只添加一个参数,试试这个

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("headless", "window-size=1200,600");
ChromeDriver driver = new ChromeDriver(chromeOptions);
driver.get("your.app.url");
System.out.println(driver.getTitle)

答案 1 :(得分:0)

您必须考虑以下几项更改:

  • 当您执行 System.setProperty 时,会提供 absolute path 二进制文件的chromedriver

    System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
    
  • 窗口大小argument options.addArguments("window-size=1400,600");

    chromeOptions.addArguments("window-size=1400,600");
    
  • 当你 driver.get()包含 https www

    driver.get("https://www.google.co.in");
    
  • 要检索页面标题,该方法为 getTitle()

    System.out.println(driver.getTitle());
    
  • 您修改过的代码块如下所示:

    System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--headless");
    chromeOptions.addArguments("window-size=1400,600");
    WebDriver driver =  new ChromeDriver(chromeOptions);
    driver.get("https://www.google.co.in");
    System.out.println(driver.getTitle());