2

以下方法无法正常工作。由于使用了 GRID,因此这里将能力设置为

System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");   
DesiredCapabilities capability=null; 

方法一:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir=C:/Users   /username/AppData/Local/Google/Chrome/User Data/Default"));
driver = new ChromeDriver(capabilities);

方法二:

DesiredCapabilities capabilities = DesiredCapabilities.chrome(); 
String chromeProfile = "C:/Users/username/AppData/Local/Google/Chrome/Application   /chrome.exe"; 
ArrayList<String> switches = new ArrayList<String>(); 
switches.add("C:/Users/username/AppData/Local/Google/Chrome/User Data/Default" + chromeProfile); 
capabilities.setCapability("chrome.switches", switches); 
driver = new ChromeDriver(capabilities); 
4

2 回答 2

2
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);

如果你遇到这样的错误:

org.openqa.selenium.WebDriverException:未知错误:Chrome 启动失败:正常退出

然后尝试创建一个新的 Chrome 配置文件并执行测试。

  1. 复制文件夹,'User Data'
  2. 在同一位置粘贴并重命名文件夹。e.g., New User
  3. 现在,找到目录,C:/Users/user_name/AppData/Local/Google/Chrome/New User
  4. 如果您想测试配置文件,请为一些站点添加书签并在下次运行时观察它们。
于 2013-03-07T12:47:35.727 回答
0

1 在启动节点时设置 chromedriver 属性。我的做法:

java -jar selenium-server-standalone-2.31.0.jar -role node -hub http://localhost:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15 -Dwebdriver.chrome.driver=lib\chromedriver.exe

2 在代码中我的方法:

capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), capabilities);

3 加载HTTP基本认证页面:

String username = "Pavel";
String password = "Omgtoosecrettotellyou";
driver.get("http://" + username + ":" + password + "@" +"your-site.com");
于 2013-03-01T09:46:46.687 回答