2017-03-20 38 views
1

我運行下面的代碼找到元素:不能在無頭phantomjs 2.1.1硒3

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();   

可正常工作。 但現在我試着用無頭Chrome瀏覽器使用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

可否請您提供以下信息:1。什麼是你的目標是什麼? 2.你想做什麼? 3.調試你的代碼並更新你完全卡住的地方。提供錯誤的正確快照。 – DebanjanB

+0

在您嘗試查找的元素周圍發佈html代碼,並且我可能能夠使用xpath幫助您 –

+0

您的目標是什麼?檢查我的項目的無頭測試的可行性,2.你想要做什麼?無論我能在UI中做什麼,我都可以在無頭模式下做同樣的事情。 3.調試後, - 無頭模式無法找到元素By.id(「email」)),請在Facebook登錄頁面檢查用戶名的html代碼。我嘗試相同的元素,因此不張貼在這裏。錯誤截圖在帖子中更新 – Avishek2585835

回答

1

這裏是工作代碼: (這是在兩個系統輸出要注意的是登錄失敗,標題是一樣的,所以司機得到了相同的標題)

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()); 

讓我知道,如果它幫助你。

+0

@ Avishek2585835如果此答案迎合您的問題/疑問,請您接受此答案作爲解決方案以關閉討論並投票回答問題? – DebanjanB

+0

感謝它的工作。 – Avishek2585835

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);