【问题标题】:phantomjs exception error on Linux(64bit) org.openqa.selenium.NoSuchElementExceptionLinux(64位)org.openqa.selenium.NoSuchElementException上的phantomjs异常错误
【发布时间】:2016-10-01 03:56:06
【问题描述】:

代码在我的localhost(win10) 中运行良好,但在Linux(64bt) 上出现异常错误:

org.openqa.selenium.NoSuchElementException: {"errorMessage":"无法找到 id 为 'magix_vf_root' 的元素","re​​quest":{"headers":{"Accept":"application/json, image/png ","Connection":"Keep-Alive","Content-Length":"38","Content-Type":"application/json; charset=utf-8","Host":"localhost:12709"} ,"httpVersion":"1.1","method":"POST","post":"{\"using\":\"id\",\"value\":\"magix_vf_root\"}"," url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element ","re​​lative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority": "","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/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();
    }
}

结果信息:

【问题讨论】:

    标签: linux selenium selenium-webdriver phantomjs


    【解决方案1】:

    代码在我的 localhost(win10) 中运行良好,但在 Linux(64bt) 上出现异常错误:org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with id 'magix_vf_root'"

    看起来像是时间问题,您应该尝试使用WebDriverWait 等到元素出现在DOM 上,如下所示:-

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

    【讨论】:

    • 谢谢。是的,隐式和显式都适用于 windows(10) 平台,但在 Linux 上不成功;屏幕截图显示它在Linux上没有成功登录,但在windows(10)上可以。
    • 那么您使用explicit wait 时遇到的异常是什么??
    • 是的,在我的代码中是这样的:d.manage().timeouts().implicitlyWait(60L, TimeUnit.SECONDS);
    • sorry,explicit wait&implicit wait在Linux下都是同一个异常,但是在windows下运行良好
    • 服务器问题。我安装了一个新的虚拟服务器进行测试,现在效果很好。非常感谢。
    猜你喜欢
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多