【问题标题】:NoSuchElementException: no such element: Unable to locate element while trying to click dropdown element on amazon.co.uk through SeleniumNoSuchElementException:没有这样的元素:尝试通过 Selenium 在 amazon.co.uk 上单击下拉元素时无法找到元素
【发布时间】:2019-06-19 05:24:39
【问题描述】:

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

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

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

【问题讨论】:

    标签: selenium css-selectors webdriver amazon webdriverwait


    【解决方案1】:

    要展开Amazon 导航栏上的下拉菜单,您不需要click() 而是鼠标悬停 诱导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
      

    【讨论】:

      【解决方案2】:

      我在亚马逊看到的:

      'full shop directory' 不是(实际上是链接文本)。 如果元素是 <a>this is a text</a> 那么我认为它是链接文本。

      在您的情况下,文本由大量空格 **********Full Shop Directory********** 组成,而且这种片状可能会导致问题(可能在你的情况下)。

      将您的定位器更正为结构安全,然后重试。

      示例: xpath://a/span[@class='nav-text' and text()='Full Shop Directory']

      更新: 你的测试写错了,或者不知道你的实际目标是什么。

      你做什么:

      1. 点击导航菜单和“完整商店目录”的相同视图页面是 开放。链接不同。
      2. 点击“按部门购物”按钮(隐藏)
      3. 断言网址...

      建议步骤:

      1. 移动到“按部门购物”并触发下拉菜单。

      2. 点击下拉菜单中的“完整商店目录”按钮。

      3. 等待页面加载

      4. 声明页面网址。

      代码:

      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);
      
      Actions ac = new Actions(driver);
      ac.moveToElement(shopByDepartment).perform();
      
      WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
      ShopAllNav.click();
      
      Assert.assertTrue(driver.getCurrentUrl().matches("https://www.amazon.co.uk/gp/site-directory/ref=nav_shopall_fullstore"), "matches current url");
      

      代码已经过测试并确认可以正常工作。

      【讨论】:

      • 我已经尝试用 css 选择器做同样的事情(没有用)所以我认为这不是问题。
      • 您确定只有 1 个项目与定位器匹配吗?
      • 我很确定是的,定位器在上面是我的代码,第二个私有静态最终(SHOP_ALL),它非常具体
      • 你的测试写得很破旧。您单击导航菜单并打开“完整商店”页面。然后尝试单击位于 SHOP_BY_DEPARTMENT 下拉列表中的按钮。检查我的答案的更新
      【解决方案3】:

      您需要等待该特定组件可见。在它的可见性之后,您可以对其应用操作。

      你可以试试下面的代码。

      WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
      WebDriverWait wait = new WebDriverWait(driver,200);
      wait.until(ExpectedConditions.visibilityOf(ShopAllNav));
      ShopAllNav.click();
      

      【讨论】:

      • 谢谢,但它不起作用,我仍然得到同样的错误
      • 您可以使用下面的代码:WebElement ShopAllNav = driver.findElement(By.linkText(" //*[@id="nav-flyout-shopAll"]/div[2]/a/跨度"));
      • “nav-flyout-shopAll”部分显示“无法解决”
      猜你喜欢
      • 1970-01-01
      • 2019-06-08
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      相关资源
      最近更新 更多