selenium - NoSuchElementException : no such element: Unable to locate element while trying to click dropdown element on amazon. co.uk 通过 Selenium

标签 selenium css-selectors webdriver amazon webdriverwait

我正在 selenium/intelliJ/Java 中运行自动化测试。网络驱动程序应该单击亚马逊导航栏上的下拉菜单,然后单击下拉菜单中的链接之一。它正确地完成了这两件事,下拉选项指向它的链接,但是 selenium 测试本身失败了,这是错误:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Full Shop Directory"}

这是我的代码:

package com.testing.webdriver;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;


import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MyFirstTest {
    WebDriver driver = new ChromeDriver();

    @BeforeClass
    public static void setupWebdriver() {
        WebDriverManager.chromedriver().setup();
    }


    private static final By SHOP_BY_DEPARTMENT = By.cssSelector("#nav-link-shopall");
    private static final By SHOP_ALL = By.cssSelector("#nav-flyout-shopAll > div.nav-template.nav-flyout-content.nav-tpl-itemList > a");



    @Test
    public void startWebdriver() {

        driver.navigate().to("https://www.amazon.co.uk/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        WebElement shopByDepartment = driver.findElement(SHOP_BY_DEPARTMENT);
        shopByDepartment.click();

        WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
        ShopAllNav.click();

        Assert.assertTrue("matches current url",
                driver.getCurrentUrl().matches("https://www.amazon.co.uk/gp/site-directory/ref=nav_shopall_fullstore"));

    }

    @After
    public void breakdown() throws InterruptedException {
        Thread.sleep(20000);
        driver.close();
    }

测试应该按照我所说的那样通过。正如错误所说,我认为这与下拉菜单中的链接有关,但我仍然不知道如何纠正这个问题。任何帮助将不胜感激,谢谢。

最佳答案

展开 Amazon 上的下拉菜单导航栏你不需要 click() 而是 Mouse Hover 诱导 WebDriverWait 你可以使用下面的解决方案:

  • 代码块:

    System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions"); 
    WebDriver driver =  new ChromeDriver(options);
    driver.get("https://www.amazon.co.uk/");
    new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#nav-shop>a#nav-link-shopall")))).perform();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.nav-catFlyout.nav-flyout div.nav-template.nav-flyout-content.nav-tpl-itemList a"))).click();
    Assert.assertTrue(driver.getCurrentUrl().matches("https://www.amazon.co.uk/gp/site-directory/ref=nav_shopall_fullstore"));
    driver.quit();
    
  • 控制台输出:

    Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 41299
    Only local connections are allowed.
    Jan 25, 2019 5:41:24 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    

关于selenium - NoSuchElementException : no such element: Unable to locate element while trying to click dropdown element on amazon. co.uk 通过 Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54364210/

相关文章:

python - 如何在 Python 中使用 Selenium WebDriver 指定 Firefox 命令行选项?

javascript - 如何在 Javascript 中更改关键帧的 CSS 属性?

css - 将最后一个 child 应用于元素不起作用

selenium - 如何通过 Java 中的 Selenium/Webdriver 单击复选框列表中的复选框?

java - 通过 maven 运行 testng.xml 出现错误

python - 尝试创建一个机器人来与 Selenium Python 连接不和谐

java - TestNG+Cucumber 并行测试在同一 chrome 实例上运行

c# - Selenium Webdriver C#, Chrome, 图标隐藏元素且不可点击

c# - Selenium:无法访问 iframe 和其中的数据

jquery - 如何找到所有未选中的复选框?