我有下一个代码,这个代码适用于chrome驱动程序,但适用于phantom js 1.4.4库和2.1.1驱动程序,我无法定位元素
铬驱动程序不会出现此问题。
代码试用:
public void test1()
{
DesiredCapabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot",true);
((DesiredCapabilities) caps).setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/Users/santiagogalicia/downloads/phantomjs");
caps.setJavascriptEnabled(true);
String [] phantomJsArgs = {"--web-security=no", "--ignore-ssl-errors=yes"};
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);
WebDriver driver = new PhantomJSDriver(caps);
Dimension dimension = new Dimension(400,600);
driver.manage().window().setSize(dimension);
driver.get("https://stage-commissionist.payclip.com/#/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("formUsername")));
driver.findElement(By.id("formUsername")).sendKeys(User);
driver.findElement(By.id("formPassword")).sendKeys(Password);
driver.findElement(By.cssSelector(".btn")).click();
driver.close();
}
我尝试更换驱动程序,并与其他驱动程序一起工作
我看到的错误:
[ERROR - 2019-02-07T19:15:26.476Z] Session [b736bad0-2b0c-11e9-b0db-6d1517ea5006] - page.onError - msg: ReferenceError: Can't find variable: Set phantomjs://platform/console++.js:263 in error
[ERROR - 2019-02-07T19:15:26.476Z] Session [b736bad0-2b0c-11e9-b0db-6d1517ea5006] - page.onError - stack:
(anonymous function) (https://stage-commissionist.payclip.com/static/js/1.ea7f0607.chunk.js:1)
f (https://stage-commissionist.payclip.com/#/:1)
phantomjs://platform/console++.js:263 in error
[ERROR - 2019-02-07T19:15:26.759Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1549566926721
正如您提到的,此代码适用于chrome驱动程序,但适用于phantom js 1.4.4库和2.1.1此错误消息。。。
[ERROR - 2019-02-07T19:15:26.476Z] Session [b736bad0-2b0c-11e9-b0db-6d1517ea5006] - page.onError - msg: ReferenceError: Can't find variable: Set phantomjs://platform/console++.js:263 in error
[ERROR - 2019-02-07T19:15:26.476Z] Session [b736bad0-2b0c-11e9-b0db-6d1517ea5006] - page.onError - stack: (anonymous function) (https://stage-commissionist.payclip.com/static/js/1.ea7f0607.chunk.js:1) f (https://stage-commissionist.payclip.com/#/:1)
phantomjs://platform/console++.js:263 in error [ERROR - 2019-02-07T19:15:26.759Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1549566926721
意味着PhantomJSDriver未正确启动。
根据ReferenceError:找不到变量:Set中的讨论,主要原因似乎是PhantomJS的前一个版本不支持ES2015Set
。
解决方案
您可以通过安装uncss-jsdom
来尝试uncss
的实验分支,该分支使用jsdom而不是PhantomJS。您可以在#265中找到合并:用jsdom替换PhantomJS。在这里你可以找到关于Think jsdom 的讨论
然而,使用Seleniumv3.14.0和phantomjsdriver-1.44.jar,您的代码块初始化phantomjsdriver和Ghost Browser,您可以使用以下解决方案:
代码块:
package phantomJSDriver; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.DesiredCapabilities; public class A_PhantomJS_DCap { public static void main(String[] args) { DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); desiredCapabilities.setJavascriptEnabled(true); desiredCapabilities.setCapability("takesScreenshot", true); desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe"); String [] phantomJsArgs = {"--web-security=no", "--ignore-ssl-errors=yes"}; desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs); WebDriver driver = new PhantomJSDriver(desiredCapabilities); Dimension dimension = new Dimension(400,600); driver.manage().window().setSize(dimension); driver.get("https://www.google.co.in"); System.out.println(driver.getTitle()); driver.quit(); } }
控制台输出:
Feb 09, 2019 8:35:12 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> INFO: executable: C:Utilityphantomjs-2.1.1-windowsbinphantomjs.exe Feb 09, 2019 8:35:12 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> INFO: port: 18249 Feb 09, 2019 8:35:12 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> INFO: arguments: [--web-security=no, --ignore-ssl-errors=yes, --webdriver=18249, --webdriver-logfile=C:UsersAtechM_03LearnAutmationlearn-automationphantomjsdriver.log] Feb 09, 2019 8:35:12 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> INFO: environment: {} [INFO - 2019-02-09T15:05:14.986Z] GhostDriver - Main - running on port 18249 [INFO - 2019-02-09T15:05:16.008Z] Session [1b791e00-2c7c-11e9-9e77-ef7d90721101] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":false} [INFO - 2019-02-09T15:05:16.008Z] Session [1b791e00-2c7c-11e9-9e77-ef7d90721101] - page.customHeaders: - {} [INFO - 2019-02-09T15:05:16.008Z] Session [1b791e00-2c7c-11e9-9e77-ef7d90721101] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-8-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}} [INFO - 2019-02-09T15:05:16.008Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 1b791e00-2c7c-11e9-9e77-ef7d90721101 Feb 09, 2019 8:35:16 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS Google [INFO - 2019-02-09T15:05:20.866Z] ShutdownReqHand - _handle - About to shutdown
注意:根据您的评论,该网站仅可通过vpn使用,它是一个私人网站,因此相关代码已使用
https://www.google.co.in
进行了测试