我正在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();
}
正如我所说的,测试应该通过。正如错误所言,我认为这与下拉菜单中的链接有关,但我仍然不知道该如何解决。任何帮助将不胜感激,谢谢。
答案 0 :(得分:1)
我在亚马逊看到的东西:
不是“完整商店目录”(实际上是链接文本)。
如果该元素是 <a>this is a text</a>
,那么我将其视为链接文本。
在您的情况下,文本是用大量空格组成的********完整商店目录**********,而且这种片状现象可能会引起问题(可能就您而言)。
将定位器更正为结构安全,然后重试。
示例:
xpath://a/span[@class='nav-text' and text()='Full Shop Directory']
更新: 您的测试写错了或者根本不知道您的实际目标是什么。
您的工作
建议步骤:
转到“按部门购物”并触发下拉菜单。
从下拉菜单中单击“完整商店目录”按钮。
等待页面加载
声明页面网址。
代码:
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");
该代码已经过测试,并确认可以正常工作。
答案 1 :(得分:1)
要展开Amazon导航栏上的 Dropdown菜单,您不需要 click()
而不是 Mouse Hover em>诱导 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 :(得分:0)
您需要等待该特定组件可见。可见后,您可以对其执行操作。
您可以尝试下面的代码。
WebElement ShopAllNav = driver.findElement(By.linkText("Full Shop Directory"));
WebDriverWait wait = new WebDriverWait(driver,200);
wait.until(ExpectedConditions.visibilityOf(ShopAllNav));
ShopAllNav.click();