代码在localhost(win10)
中运行良好,但Linux(64bt)
上的异常错误:
org.openqa.selenium.NoSuchElementException:{“errorMessage”:“无法找到id为'magix_vf_root'的元素”,“请求”:{“headers”:{“接受”:“application / json,image / png “,”Connection“:”Keep-Alive“,”Content-Length“:”38“,”Content-Type“:”application / json; charset = utf-8“,”Host“:”localhost:12709“} “httpVersion”: “1.1”, “方法”: “POST”, “后”: “{\” 使用\ “:\” ID \ “\ ”值\“:\ ”magix_vf_root \“}”,”网址 “:”/元素 “ ”urlParsed“:{ ”锚“: ”“, ”查询“: ”“, ”文件“: ”元素“, ”目录“: ”/“, ”路径“:”/元素”, “相对的”: “/元件”, “端口”: “”, “宿主”: “”, “密码”: “”, “用户”: “”, “用户信息”: “”, “权威”: “”, “协议”: “”, “源”: “/元件”, “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();
}
}
结果信息:
答案 0 :(得分:0)
代码在我的localhost(win10)中运行良好,但Linux上的异常错误(64bt):org.openqa.selenium.NoSuchElementException:{&#34; errorMessage&#34;:&#34;无法找到元素id&#39; magix_vf_root&#39;&#34;
看起来像时间问题,您应该尝试使用WebDriverWait
等待DOM
上的元素出现,如下所示: -
WebElement quick = new WebDriverWait(d, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("magix_vf_root")));