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

前端 未结 3 1823
情书的邮戳
情书的邮戳 2021-01-13 03:16

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) th         


        
相关标签:
3条回答
  • 2021-01-13 03:37

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-13 03:40

    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']
    
    0 讨论(0)
  • 2021-01-13 03:59
    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)));
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题