使用所需功能设置Firefox二进制文件

时间:2017-11-24 12:54:21

标签: selenium firefox

抱歉可怕的格式化...我还是没弄明白如何格式化......

我正在尝试使用Selenium启动firefox。 Firefox安装在AppData文件夹中。因此,我尝试使用下面的代码

在Desired Capability对象上设置二进制路径
System.setProperty("webdriver.gecko.driver","C:\\path\\geckodriver.exe");
String pathToBinary = "C:\\Users\\me\\AppData\\Local\\Microsoft\\AppV\\Client\\Integration\\D90C0155-81ED-4977-B52D-E34EAA24FB3C\\Root\\VFS\\ProgramFilesX86\\Mozilla Firefox\\firefox.exe";
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testProfile = new FirefoxProfile();
testProfile.setAcceptUntrustedCertificates(true);
testProfile.setAssumeUntrustedCertificateIssuer(true);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testProfile);
dc.setCapability(FirefoxDriver.BINARY, pathToBinary);
driver = new FirefoxDriver(dc);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
//driver.manage().window().setSize(new Dimension(1920, 1080));

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
System.out.println("beforee driver.get");
driver.get("https://www.google.co.za");

我这里有两个问题..

1. Getting below error on - driver.manage().window().maximize();
org.openqa.selenium.WebDriverException: Failed to find width field
Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:54:15.039Z'
System info: host: 'N0610114502', ip: '10.9.21.178', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
Driver info: org.openqa.selenium.firefox.FirefoxDriver

2. If I comment out that line and run the program, i get the below error
org.openqa.selenium.InvalidArgumentException: 
Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:54:15.039Z'
System info: host: 'N0610114502', ip: '10.9.21.178', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\cc305718\AppData\Local\Temp\rust_mozprofile.sIobxPYVJ8iA, rotatable=false, timeouts={implicit=0, page load=300000, script=30000}, pageLoadStrategy=normal, platform=XP, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=52.5.0, platformVersion=6.1, moz:processID=6712, browserName=firefox, javascriptEnabled=true, platformName=XP}]
Session ID: 030d91eb-9219-43c9-9ade-210005b150b8

这是否意味着我以错误的方式设置Binary?如果是这样,正确的用法是什么?

我不想使用下面的描述方法

FirefoxProfile profile = new FirefoxProfile(); 
FirefoxBinary binary = new FirefoxBinary(new File("C:\\path to firefox\\firefox.exe")); 
driver = new FirefoxDriver(binary, profile);

1 个答案:

答案 0 :(得分:1)

对于你看到的第一个错误:

org.openqa.selenium.WebDriverException: Failed to find width field

我相信这发生在你身上,因为你要求Selenium最大化浏览器窗口而不告诉它在浏览器窗口最大化时你想要的大小。所以它失败了“找不到宽度字段”,因为你之前没有实际设置宽度参数。

因此,您需要在下面的代码行之前调用以最大化窗口(您目前已在上面的示例代码中对此进行了注释)。

driver.manage().window().setSize(new Dimension(1920, 1080));

尝试修复第一个错误,如果您仍有任何问题,请随时问我。如果可以的话,很高兴能提供帮助!

更新1:

我的下一个建议是在您创建WebDriver实例时稍微重新排序代码。我只想确保所有内容都处于正确的顺序(我之前看到过与Selenium语句顺序有关的问题)以及是否仍然看到错误:

driver = new FirefoxDriver(dc);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("https://www.google.co.za");
driver.manage().window().setSize(new Dimension(1920, 1080));
driver.manage().window().maximize();

更新2:

好的我很满意如何我们现在正在调用你的WebDriver实例,所以你在设置gecko驱动程序方面有些不妥。

我注意到您正在为这两行中的实际Firefox安装设置二进制文件:

String pathToBinary = "C:\\Users\\me\\AppData\\Local\\Microsoft\\AppV\\Client\\Integration\\D90C0155-81ED-4977-B52D-E34EAA24FB3C\\Root\\VFS\\ProgramFilesX86\\Mozilla Firefox\\firefox.exe";
dc.setCapability(FirefoxDriver.BINARY, pathToBinary);

如果Firefox二进制文件的路径不在PATH上,则只会设置这些。因此,如果它们在你的PATH上,你可以注释掉/删除这两行,如果没有,你可以将它们保留在那里。

您还应将marionette功能设置为true,如下所示:

dc.setCapability("marionette", true);

所以,只是为了总结一下您现在应该如何设置WebDriver实例:

DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(dc);
driver.get("https://www.google.co.za");