PhantomJSDriver 找到了 Element 但仍未定义
PhantomJSDriver found Element but is still undefined
我在使用 PhantomJSDriver 和 Java,
时遇到了一个奇怪的问题
我正在尝试单击这样的 link:
WebElement loginButton = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath("//a[@ui-sref='login']"))
);
LOG.debug("Login Button Found : ["+loginButton.getAttribute("href")+"]" );
loginButton.click();
使用 Crome 驱动程序我没有遇到任何问题,但是使用 phantomsJS(2.0.0(或 1.9.7/1.9.8)和 GhostDriver 1.2.0 我遇到了这种错误:
[DEBUG - 2015-05-04T09:46:03.610Z] WebElementLocator - _handleLocateCommand - Element(s) Search Start Time: 1430732763610
[DEBUG - 2015-05-04T09:46:03.612Z] WebElementLocator - _find.locator - {"using":"xpath","value":"//a[@ui-sref='login']"}
[DEBUG - 2015-05-04T09:46:03.624Z] WebElementLocator - _locateElement.locator - {"using":"xpath","value":"//a[@ui-sref='login']"}
[DEBUG - 2015-05-04T09:46:03.624Z] WebElementLocator - _locateElement.findElementResult - {"status":0,"value":{"ELEMENT":":wdc:1430732763623"}}
[DEBUG - 2015-05-04T09:46:03.624Z] WebElementLocator - _handleLocateCommand.elements - {"status":0,"value":{"ELEMENT":":wdc:1430732763623"}}
[DEBUG - 2015-05-04T09:46:03.624Z] WebElementLocator - _handleLocateCommand.rootElement - BODY
[DEBUG - 2015-05-04T09:46:03.625Z] WebElementLocator - _handleLocateCommand - Element(s) Found. Search Stop Time: 1430732768610
错误信息:
org.openqa.selenium.WebDriverException: {"errorMessage":"undefined is not an object (evaluating '(y(a)?y(a).parentWindow||y(a).defaultView:window).getComputedStyle(a,null).MozTransform.match')","request":{"headers":{"Accept":"application/json, image/png","Cache-Control":"no-cache","Connection":"Keep-Alive","Host":"localhost:7356"},"httpVersion":"1.1","method":"GET","url":"/displayed","urlParsed":{"anchor":"","query":"","file":"displayed","directory":"/","path":"/displayed","relative":"/displayed","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/displayed","queryKey":{},"chunks":["displayed"]},"urlOriginal":"/session/5f287dd0-f242-11e4-9753-1726c362b9fc/element/%3Awdc%3A1430732763623/displayed"}}
Command duration or timeout: 229 milliseconds
我在 etc 之前添加了显式等待,但它什么也没做...
奇怪的是,如果我改用这段代码:
d.findElement(By.xpath("//a[@ui-sref='login']"));
LOG.debug("Login Button Found : ["+loginButton.getAttribute("href")+"]" );
loginButton.click();
我可以访问 href 属性!
但是 click() 方法失败
[编辑]
第一个 PhanstomJS 配置如下所示:
// Change "User-Agent" via page-object capabilities
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", "My User Agent - Chrome");
// Disable "web-security", enable all possible "ssl-protocols" and "ignore-ssl-errors" for PhantomJSDriver
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {
"--web-security=false",
"--ssl-protocol=any",
"--ignore-ssl-errors=true",
"--webdriver-loglevel=DEBUG"
});
// Fetch configuration parameters
// "phantomjs_exec_path"
if (sConfig.getProperty("phantomjs_exec_path") != null) {
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, sConfig.getProperty("phantomjs_exec_path"));
} else {
throw new IOException(String.format("Property '%s' not set!", PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY));
}
}
感谢您的帮助!
您可以为 PhantomJS 尝试以下配置:
DesiredCapabilities capabilities;
capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "drivers/phantomjs.exe");
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX,"Y");
capabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0");
//intialize driver and set capabilties
driver = new PhantomJSDriver(capabilities);
此外,尝试以这种方式查找元素:
wait.until(ExpectedConditions.presenceOfElementLocated(byExpression));
我在使用 PhantomJSDriver 和 Java,
时遇到了一个奇怪的问题
我正在尝试单击这样的 link:
WebElement loginButton = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath("//a[@ui-sref='login']"))
);
LOG.debug("Login Button Found : ["+loginButton.getAttribute("href")+"]" );
loginButton.click();
使用 Crome 驱动程序我没有遇到任何问题,但是使用 phantomsJS(2.0.0(或 1.9.7/1.9.8)和 GhostDriver 1.2.0 我遇到了这种错误:
[DEBUG - 2015-05-04T09:46:03.610Z] WebElementLocator - _handleLocateCommand - Element(s) Search Start Time: 1430732763610
[DEBUG - 2015-05-04T09:46:03.612Z] WebElementLocator - _find.locator - {"using":"xpath","value":"//a[@ui-sref='login']"}
[DEBUG - 2015-05-04T09:46:03.624Z] WebElementLocator - _locateElement.locator - {"using":"xpath","value":"//a[@ui-sref='login']"}
[DEBUG - 2015-05-04T09:46:03.624Z] WebElementLocator - _locateElement.findElementResult - {"status":0,"value":{"ELEMENT":":wdc:1430732763623"}}
[DEBUG - 2015-05-04T09:46:03.624Z] WebElementLocator - _handleLocateCommand.elements - {"status":0,"value":{"ELEMENT":":wdc:1430732763623"}}
[DEBUG - 2015-05-04T09:46:03.624Z] WebElementLocator - _handleLocateCommand.rootElement - BODY
[DEBUG - 2015-05-04T09:46:03.625Z] WebElementLocator - _handleLocateCommand - Element(s) Found. Search Stop Time: 1430732768610
错误信息:
org.openqa.selenium.WebDriverException: {"errorMessage":"undefined is not an object (evaluating '(y(a)?y(a).parentWindow||y(a).defaultView:window).getComputedStyle(a,null).MozTransform.match')","request":{"headers":{"Accept":"application/json, image/png","Cache-Control":"no-cache","Connection":"Keep-Alive","Host":"localhost:7356"},"httpVersion":"1.1","method":"GET","url":"/displayed","urlParsed":{"anchor":"","query":"","file":"displayed","directory":"/","path":"/displayed","relative":"/displayed","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/displayed","queryKey":{},"chunks":["displayed"]},"urlOriginal":"/session/5f287dd0-f242-11e4-9753-1726c362b9fc/element/%3Awdc%3A1430732763623/displayed"}}
Command duration or timeout: 229 milliseconds
我在 etc 之前添加了显式等待,但它什么也没做... 奇怪的是,如果我改用这段代码:
d.findElement(By.xpath("//a[@ui-sref='login']"));
LOG.debug("Login Button Found : ["+loginButton.getAttribute("href")+"]" );
loginButton.click();
我可以访问 href 属性!
但是 click() 方法失败
[编辑] 第一个 PhanstomJS 配置如下所示:
// Change "User-Agent" via page-object capabilities
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", "My User Agent - Chrome");
// Disable "web-security", enable all possible "ssl-protocols" and "ignore-ssl-errors" for PhantomJSDriver
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {
"--web-security=false",
"--ssl-protocol=any",
"--ignore-ssl-errors=true",
"--webdriver-loglevel=DEBUG"
});
// Fetch configuration parameters
// "phantomjs_exec_path"
if (sConfig.getProperty("phantomjs_exec_path") != null) {
sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, sConfig.getProperty("phantomjs_exec_path"));
} else {
throw new IOException(String.format("Property '%s' not set!", PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY));
}
}
感谢您的帮助!
您可以为 PhantomJS 尝试以下配置:
DesiredCapabilities capabilities;
capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "drivers/phantomjs.exe");
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX,"Y");
capabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0");
//intialize driver and set capabilties
driver = new PhantomJSDriver(capabilities);
此外,尝试以这种方式查找元素:
wait.until(ExpectedConditions.presenceOfElementLocated(byExpression));