5

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.

Dherik
  • 17,757
  • 11
  • 115
  • 164
user3030202
  • 51
  • 1
  • 1
  • 5
  • try increasing timeout. make it 100 seconds and see what happens. – R Dhaval Jul 05 '17 at 08:10
  • Getting this now: 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 100 second(s) with 500 MILLISECONDS interval) – user3030202 Jul 05 '17 at 08:33
  • 2
    This usually happens when you have a *loadng* overlay placed while page is loading. In such cases selenium cannot find elements after *loading* disappears. Do you have any such overlay in place? – R Dhaval Jul 05 '17 at 08:43
  • Yes , an overlay also opens when user mouse hover on SIgn in link. But clicking on sign in will redirect the user to sign in page. So what to do in this case? – user3030202 Jul 05 '17 at 08:51
  • wait for the invisibility of the overlay :) – R Dhaval Jul 05 '17 at 09:14

4 Answers4

2

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;
    }
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
Sudha Velan
  • 633
  • 8
  • 24
1

Here is the Answer to your Question:

The xpath you have constructed //a[@data-val='ma_signin'] is not unique. The xpath matches with 3 nodes. If you want to click on Sign In button you can consider using this unique xpath:

//div[@id='J_SC_header']//div[@class='sc-hd-row sc-hd-main']//a[@rel='nofollow'][@data-val='ma_signin']
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I always get issues regarding element not found or element not present and this is most likely due to xpath issues. How can i get expert in finding the xpath? Though i am using the same xpath in firepath while inspecting the element in firebug and element is getting inspect. – user3030202 Jul 05 '17 at 10:14
  • @user3030202 Good Question :) `xpath` provided by `firepath` is `absolute xpath` and is your reference to construct a unique `logical xpath`. Thanks – undetected Selenium Jul 05 '17 at 10:18
  • E.g. i am now on this form https://login.alibaba.com/?spm=a2700.8293689.0.0.NdeZUw&tracelog=hd_signin and trying to access the Email field to enter email using this xpath , but again getting element not found. xpath(".//*[@id='fm-login-id']") also tried this as well but no luck xpath("//input[@name='loginId']") – user3030202 Jul 05 '17 at 10:21
1
public static void waitVisibilityOfElementLocated(WebDriver driver, String locator) {
        String key = "";
        WebElement element = null;
        try {
            key = Utility.fetchLocatorKey(locator);
        } catch (Exception e) {
            System.out.println("Exception in getText method, " + e.getMessage());
        }

        if (key.endsWith("id")) {
            WebDriverWait wait = new WebDriverWait(driver, 60);
            element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(locator)));
        } else if (key.endsWith("cssselector")) {
            WebDriverWait wait = new WebDriverWait(driver, 60);
            element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(locator)));
        } else if (key.endsWith("linktext")) {
            WebDriverWait wait = new WebDriverWait(driver, 60);
            element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(locator)));
        } else if (key.endsWith("xpath")) {
            WebDriverWait wait = new WebDriverWait(driver, 60);
            element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(locator)));
        }

    }
Bhavesh Soni
  • 188
  • 8
0

I lived same issue in my local, and then I realized made screen size 125% in display settings. After changed to 100% the element could be located. "TimeoutException: Expected condition failed: waiting for visibility of [[RemoteWebDriver: chrome on WINDOWS (8ec6c...)] -> xpath: //span[@class = 'v-button ...] (tried for 30 second(s) with 1000 milliseconds interval)

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 24 '22 at 19:47