gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:49:39 25 4
gpt4 key购买 nike

我正在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com