0
votes

the code runs well in my localhost(win10), but exception error on Linux(64bt) :

org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with id 'magix_vf_root'","request":{"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","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/350bd6e0-8784-11e6-b57c-35629091c8a9/element"}} Command duration or timeout: 60.32 seconds

Here is my code:

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

And the result message:

the result message

1

1 Answers

0
votes

the code runs well in my localhost(win10),but exception error on Linux(64bt): org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with id 'magix_vf_root'"

It looks like timing issue, you should try using WebDriverWait to wait until element is present on the DOM as below :-

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