NoSuchElementException:没有这样的元素:尝试通过 Selenium 单击 amazon.co.uk 上的下拉元素时无法定位元素
NoSuchElementException: no such element: Unable to locate element while trying to click dropdown element on amazon.co.uk through Selenium
我是 运行 selenium/intelliJ/Java 的自动化测试。网络驱动程序应该单击亚马逊导航栏上的下拉菜单,然后单击下拉菜单中的 link 之一。它正确地完成了这两件事,下拉选项导致它的 link,但是 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();
}
测试应该按照我所说的那样通过。正如错误所说,我认为这与下拉菜单中的 link 有关,但我仍然不知道如何纠正这个问题。如有任何帮助,我们将不胜感激。
我在亚马逊看到的:
'full shop directory' 不是(真正的 link 文本)。
如果元素是 <a>this is a text</a>
那么我认为它是 link 文本。
在您的情况下,文本由大量空格组成 **********完整商店目录************ 并且这种不稳定可能会导致问题(在你的情况下可能)。
将您的定位器更正为结构安全的,然后重试。
示例:
xpath: //a/span[@class='nav-text' and text()='Full Shop Directory']
更新:
你的测试写错了或者不知道你的实际目标是什么。
你的工作:
- 单击导航菜单和 'full shop directory' 相同的视图页面是
越来越开放。 link不同。
- 单击 'shop by department' 中的按钮(隐藏)
- 断言 urls...
建议的步骤:
移动到 'shop by department' 并触发下拉菜单。
单击下拉菜单中的 'full shop directory' 按钮。
等待页面加载
声明页面 url.
代码:
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");
代码已经过测试并确认可以正常工作。
您需要等待特定组件可见。在其可见性之后,您可以对其应用操作。
您可以试试下面的代码。
WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
WebDriverWait wait = new WebDriverWait(driver,200);
wait.until(ExpectedConditions.visibilityOf(ShopAllNav));
ShopAllNav.click();
要展开 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/intelliJ/Java 的自动化测试。网络驱动程序应该单击亚马逊导航栏上的下拉菜单,然后单击下拉菜单中的 link 之一。它正确地完成了这两件事,下拉选项导致它的 link,但是 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();
}
测试应该按照我所说的那样通过。正如错误所说,我认为这与下拉菜单中的 link 有关,但我仍然不知道如何纠正这个问题。如有任何帮助,我们将不胜感激。
我在亚马逊看到的:
'full shop directory' 不是(真正的 link 文本)。
如果元素是 <a>this is a text</a>
那么我认为它是 link 文本。
在您的情况下,文本由大量空格组成 **********完整商店目录************ 并且这种不稳定可能会导致问题(在你的情况下可能)。
将您的定位器更正为结构安全的,然后重试。
示例:
xpath: //a/span[@class='nav-text' and text()='Full Shop Directory']
更新: 你的测试写错了或者不知道你的实际目标是什么。
你的工作:
- 单击导航菜单和 'full shop directory' 相同的视图页面是 越来越开放。 link不同。
- 单击 'shop by department' 中的按钮(隐藏)
- 断言 urls...
建议的步骤:
移动到 'shop by department' 并触发下拉菜单。
单击下拉菜单中的 'full shop directory' 按钮。
等待页面加载
声明页面 url.
代码:
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");
代码已经过测试并确认可以正常工作。
您需要等待特定组件可见。在其可见性之后,您可以对其应用操作。
您可以试试下面的代码。
WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
WebDriverWait wait = new WebDriverWait(driver,200);
wait.until(ExpectedConditions.visibilityOf(ShopAllNav));
ShopAllNav.click();
要展开 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