我正在运行以下代码:
System.setProperty("webdriver.chrome.driver", "C:/Softwares/selenium/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver(capability);
driver.get("https://facebook.com/");
System.out.println("Page title is: " + driver.getTitle());
driver.findElement(By.id("email")).sendKeys("Administrator");
driver.findElement(By.id("pass")).sendKeys("12iso*help");
System.out.println("Enter usrname as: "+driver.findElement(By.id("email")).getText());
driver.findElement(By.id("u_0_q")).click();
System.out.println("Page title is: " + driver.getTitle());
//driver.quit();
这可以按预期工作。 但现在我尝试使用phantomjs使用无头镀铬浏览器 代码:
WebDriver driver;
File file = new File("C:/Softwares/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"C:/Softwares/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe"
);
//caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});
driver = new PhantomJSDriver(caps);
driver.get("https://facebook.com/");
System.out.println("Page title is: " + driver.getTitle());
driver.findElement(By.id("email")).sendKeys("Administrator");
driver.findElement(By.id("pass")).sendKeys("12iso*help");
System.out.println("Enter usrname as: "+driver.findElement(By.id("email")).getText());
driver.findElement(By.id("u_0_q")).click();
System.out.println("Page title is: " + driver.getTitle());
这显示了一个错误。
Error screenshot is: Mar 20, 2017 6:28:27 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: executable: C:\Softwares\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin\phantomjs.exe
Mar 20, 2017 6:28:27 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: port: 42545
Mar 20, 2017 6:28:27 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--web-security=no, --ignore-ssl-errors=yes, --webdriver=42545, --webdriver-logfile=C:\Users\Administrator\workspace\Headless\phantomjsdriver.log]
Mar 20, 2017 6:28:27 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: environment: {}
[INFO - 2017-03-21T01:28:29.851Z] GhostDriver - Main - running on port 42545
[INFO - 2017-03-21T01:28:30.400Z] Session [b0d2ce50-0dd5-11e7-b70e-830369ea41ad] - 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 - 2017-03-21T01:28:30.400Z] Session [b0d2ce50-0dd5-11e7-b70e-830369ea41ad] - page.customHeaders: - {}
[INFO - 2017-03-21T01:28:30.401Z] Session [b0d2ce50-0dd5-11e7-b70e-830369ea41ad] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-10-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 - 2017-03-21T01:28:30.401Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: b0d2ce50-0dd5-11e7-b70e-830369ea41ad
Mar 20, 2017 6:28:30 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page title is: Welcome to Facebook
[ERROR - 2017-03-21T01:29:08.179Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1490059748124
phantomjs://platform/console++.js:263 in error
Exception in thread "main" org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with id 'email'","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"30","Content-Type":"application/json; charset=utf-8","Host":"localhost:42545","User-Agent":"Apache-HttpClient/4.5.2 (Java/1.8.0_121)"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"id\",\"value\":\"email\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/b0d2ce50-0dd5-11e7-b70e-830369ea41ad/element"}}
Command duration or timeout: 30.20 seconds
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:420)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at test2.main(test2.java:37)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:142)
... 6 more
Caused by: org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with id 'email'","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"30","Content-Type":"application/json; charset=utf-8","Host":"localhost:42545","User-Agent":"Apache-HttpClient/4.5.2 (Java/1.8.0_121)"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"id\",\"value\":\"email\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/b0d2ce50-0dd5-11e7-b70e-830369ea41ad/element"}}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
System info: host: 'WINDOWS-CPRD7P5', ip: '15.146.51.224', os.name: 'Windows NT (unknown)', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121'
Driver info: driver.version: unknown
我确定,url在显示页面标题时已正确打开。但是可以请任何人帮助为什么phantomjs无法找到页面上的元素。谢谢!
答案 0 :(得分:2)
这是工作代码: (需要注意的是,在Sysout中,标题将与登录失败相同,因此驱动程序获得相同的标题)
File src = new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
driver.get("https://www.facebook.com/");
System.out.println(driver.getTitle());
Thread.sleep(2000);
driver.findElement(By.name("email")).sendKeys("Administrator");
driver.findElement(By.name("pass")).sendKeys("12iso*help");
driver.findElement(By.name("login")).click();
Thread.sleep(2000);
System.out.println("Page title is: " + driver.getTitle());
让我知道它是否对你有所帮助。
答案 1 :(得分:0)
这对我有用:
System.setProperty("phantomjs.binary.path", seleniumBrowserDriverPath);
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("locationContextEnabled", true);
caps.setCapability("applicationCacheEnabled", true);
caps.setCapability("browserConnectionEnabled", true);
caps.setCapability("localToRemoteUrlAccessEnabled", true);
caps.setCapability("locationContextEnabled", true);
caps.setCapability("takesScreenshot", true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
seleniumBrowserDriverPath);
PhantomJSDriver driver = new PhantomJSDriver(caps);