Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected condition failed: waiting for visibility of element located by By.xpath

Tags:

I am trying to click on Sign in link on site alibaba.com

This is my test case:

public class TestCase {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        String URL = "http://www.alibaba.com/";
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver",
                "D:\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(URL);
        Thread.sleep(2000);
        SignIn.SignIn_click(driver).click();
    }

}

This is object class where in am locating the web element

package PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SignIn {


    private static WebElement element = null;

    public static WebElement SignIn_click(WebDriver driver) {
        element = (new WebDriverWait(driver, 10)).until(ExpectedConditions
                .visibilityOfElementLocated(By
                        .xpath("//a[@data-val='ma_signin']")));
        element = driver.findElement(By
                .xpath("//a[@data-val='ma_signin']"));
        return element;

    }

}

But when I run this code , I'm always getting this exception:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //a[@data-val='ma_signin'] (tried for 10 second(s) with 500 MILLISECONDS interval)
    Build info: version: 'unknown', revision: '86a5d70', time: '2017-02-16 07:47:51 -0800'
    System info: host: 'ANUM-PC', ip: '172.16.11.162', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41), userDataDir=C:\Users\Anum\AppData\Local\Temp\scoped_dir1716_14873}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.115, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
    Session ID: d0c1083c113270bd4ded08846544878e
        at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:257)
        at PageObjects.SignIn.SignIn_click(SignIn.java:15)
        at AutomationFramework.TestCase.main(TestCase.java:24)

Please help me on this.

like image 605
user3030202 Avatar asked Jul 05 '17 07:07

user3030202


People also ask

How do you handle no such element unable to locate an element?

The exception occurs when WebDriver is unable to find and locate elements. Usually, this happens when the tester writes incorrect element locator in the findElement(By, by) method. with the spaces and verify using Try XPath then we can avoid this exception. driver.

Is element visible in selenium?

We can check if an element exists with Selenium webdriver. There are multiple ways to check it. We shall use the explicit wait concept in synchronization to verify the visibility of an element. Let us consider the below webelement and check if it is visible on the page.

What is proxy element in selenium?

A proxy acts as an intermediary between clients sending requests and server responding. The primary use of a proxy is to maintain privacy and encapsulation between multiple interactive systems. A proxy can also add another layer of security on the web by acting as a firewall between Client and web servers.


1 Answers

Please try this following:

package PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SignIn {

    private static WebElement element = null;
    public static WebElement SignIn_click(WebDriver driver) throws InterruptedException {
    element = driver.findElement(By.xpath("id('J_SC_header')/header/div[2]//span[1]/a[@data-val='ma_signin']"));

    while (!isDisplayed(element)) 
    {
        Thread.sleep(3000);
        System.out.println("Element is not visible yet");
    }
    return element;

    }
    public static boolean isDisplayed(WebElement element) {
        try {
            if(element.isDisplayed())
                return element.isDisplayed();
            }catch (NoSuchElementException ex) {
            return false;
        }
        return false;
    }
}
like image 139
Sudha Velan Avatar answered Oct 11 '22 13:10

Sudha Velan