2016-10-01 153 views
0

代碼運行良好,在我localhost(win10),但Linux(64bt)異常錯誤:phantomjs異常錯誤

org.openqa.selenium.NoSuchElementException:{ 「的errorMessage」 :「無法找到id爲'magix_vf_root','request'的元素:{」headers「:{」Accept「:」application/json,image/png「,」Connection「:」Keep-Alive「,」Content-長度「:」38「,」Content-Type「:」application/json; charset = utf-8「,」Host「:」localhost:12709「},」httpVersion「:」1.1「,」method「:」POST 「 」後「: 」{\「 使用\ 」:\「 ID \」,\ 「值\」:\ 「magix_vf_root \」} 「 」URL「: 」/元件「, 」urlParsed「:{」 錨「:」」, 「查詢」: 「」, 「文件」: 「元件」, 「目錄」: 「/」, 「路徑」: 「/元件」, 「相對的」: 「/元件」, 「端口」: 「」, 「主持人」: 「」,「p assword 「:」」, 「用戶」: 「」, 「用戶信息」: 「」, 「權威」: 「」, 「協議」: 「」, 「源」: 「/元件」, 「queryKey」:{}, 「塊」:[ 「元件」]} 「urlOriginal」: 「/會話/ 350bd6e0-8784-11e6-b57c-35629091c8a9 /元件」}} 命令持續時間或超時:60.32秒

這是我的代碼:

public static void testPhantomjs() throws IOException{ 

    DesiredCapabilities caps = new DesiredCapabilities(); 
    caps.setJavascriptEnabled(true); 
    caps.setCapability("takesScreenshot", true); 
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "F:\\selenium\\phantomjs.exe"); 
    //caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/opt/phantomjs"); 
    //System.setProperty("webdriver.chrome.driver", "F:\\selenium\\phantomjs.exe"); 
    WebDriver d = new PhantomJSDriver(caps); 
    try{ 
     //d.manage().timeouts().setScriptTimeout(20L, TimeUnit.SECONDS); 
     d.manage().timeouts().implicitlyWait(60L, TimeUnit.SECONDS); 
     d.get("http://www.alimama.com/member/login.htm?forward=http%3A%2F%2Fpub.alimama.com%2Fmyunion.htm"); 
     d.manage().window().maximize(); 

     d = d.switchTo().frame(d.findElement(By.name("taobaoLoginIfr"))); 
     //WebElement quick = new WebDriverWait(d, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("J_Quick2Static"))); 
     //new Actions(d).moveToElement(quick).click().perform(); 
     //quick.click(); 
     d.findElement(By.id("J_Quick2Static")).click(); 

     //d.findElement(By.id("TPL_username_1")).clear(); 
     d.findElement(By.id("TPL_username_1")).sendKeys("username"); 
     //d.findElement(By.id("TPL_password_1")).clear(); 
     d.findElement(By.id("TPL_password_1")).sendKeys("password"); 

     getScreenshot(d, "1"); 

     d.findElement(By.id("J_SubmitStatic")).click(); 

     //d.switchTo().defaultContent(); 

     getScreenshot(d , "2"); 

     d.findElement(By.id("magix_vf_root")); 

     getScreenshot(d , "3"); 

     Set<Cookie> cookies = d.manage().getCookies(); 
     StringBuffer sb = new StringBuffer(); 
     for (Cookie cookie : cookies) { 
      sb.append(cookie.getName() + "=" + cookie.getValue() + ";"); 
     } 
     System.out.println("cookiestr:" + sb.toString()); 

    }catch(Exception e){ 
     e.printStackTrace(); 
    }finally{ 
     getScreenshot(d, "finally"); 
     d.quit(); 
    } 
} 

而結果消息:

the result message

回答

0

代碼運行以及在我的本地主機(win10),但在Linux(64bt)異常錯誤:org.openqa.selenium.NoSuchElementException:{ 「的errorMessage」: 「無法找到ID爲 'magix_vf_root' 元素」

它看起來像時機的問題,你應該嘗試使用WebDriverWait等到元素存在於DOM如下: -

WebElement quick = new WebDriverWait(d, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("magix_vf_root"))); 
+0

謝謝。是的,隱式和顯式兩者在Windows(10)平臺上都能很好地工作,但在Linux上不成功;截圖顯示,它並未在Linux上成功登錄,但在Windows(10)上正常運行。 – apperban

+0

那麼你使用'顯式等待'的異常是什麼? –

+0

是的,它在我的代碼是這樣的:'d.manage()。timeouts()。implicitlyWait(60L,TimeUnit.SECONDS);' – apperban