java - 如何在 Amazon 中使用 Selenium 和 Java 解决 org.openqa.selenium.ElementNotVisibleException

标签 java selenium selenium-webdriver xpath webdriver

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

    }

}
<小时/>

我正在使用上面的代码登录亚马逊。我收到登录按钮的 Element NotVisibleException,该异常在将鼠标悬停在您的订单上后出现。如何解决此问题

最佳答案

尝试在 Amazon 中登录时,您收到 SignIn 按钮的 ElementNotVisibleException,因为您采用的定位器策略无法唯一标识登录按钮。

要在 http://www.amazon.in登录按钮上click(),您可以使用以下代码行:

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

关于java - 如何在 Amazon 中使用 Selenium 和 Java 解决 org.openqa.selenium.ElementNotVisibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48358375/

相关文章:

python - 使用 Selenium 和 PhantomJS 单击按钮并填写表单

selenium - Go Lang 中的自动化 - 如何使用像 Selenium 这样的浏览器自动化?

xpath - 如何将键发送到 &lt;textarea id ="description"wrap ="virtual"/> 等元素

java - 如何在 JavaFX Scene Builder 中编辑 CubicCurve?

java - 如何防止列表字段自动刷新后显示重复数据

java - Runtime.exec() 在 tomcat/web 应用程序上不能正常工作

java - 读取build.xml中的多个属性文件

python - 如何将结果仅写入测试套件selenium中的一个文件

java - 无法捕获异常 NoSuchElementException

java - 如何在动态定义的 Runnable 内部使用它