How to resolve org.openqa.selenium.ElementNotVisib

2020-04-19 05:37发布

问题:

package newpackage;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class OpenAmazon {

    public static void main(String[] args) {

        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver_win32\\chromedriver.exe");
        driver=new ChromeDriver();

        driver.get("http://www.amazon.in");

        List<WebElement> yourOrders= driver.findElements(By.cssSelector("span[class='nav-line-2']"));

    //third element is the your orders
        WebElement yourOrder=yourOrders.get(2);
    //mouse hover on it to get the sign button
        Actions act=new Actions(driver);    
        act.moveToElement(yourOrder).build();   

        //click on SignIn button
        List<WebElement> signIn= driver.findElements(By.cssSelector("span[class='nav-action-inner']"));
        signIn.get(0).click();

    }

}

i am using above code to Sign In in Amazon.i am getting Element NotVisibleException for SignIn Button which appears after hover on yourOrders.how to resolve this

回答1:

While trying to Sign In in Amazon you were getting ElementNotVisibleException for SignIn Button as the Locator Strategy you had adapted wasn't uniquely identifying the Sign In button.

To click() on Sign In button in http://www.amazon.in you can use the following line of code :

driver.findElement(By.xpath("//a[@class='nav-a nav-a-2' and @id='nav-link-yourAccount']/span[@class='nav-line-1']")).click();