首页 > 解决方案 > 试图将鼠标悬停在菜单上并单击子菜单中的链接,但没有运气

问题描述

我正在学习将鼠标悬停在菜单上,然后单击子菜单中的链接。

该方案是转到 URL“ https://www.amazon.in ”,将鼠标悬停在“Hello Sign in”上,然后单击“Start here”链接。

我可以使用 moveToElement() 将鼠标悬停在“Hello Sign in”上,并且子菜单正在打开,但无法单击“从这里开始”链接。

这是我的代码。

WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();

WebElement startHere = 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Start here")));
startHere.click();

标签: seleniumselenium-webdriver

解决方案


要访问 URL https://www.amazon.in,请将鼠标悬停在Hello Sign in上,然后单击链接从此处开始。您必须为文本为Hello Sign in的元素诱导WebDriverWait可见,然后将鼠标悬停在其上,然后为文本为Start here的元素诱导WebDriverWait 。可点击,您可以使用以下解决方案:

  • 代码块:

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.amazon.in/");
    WebElement signUp = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("nav-link-yourAccount")));
    new Actions(driver).moveToElement(signUp).build().perform();
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("Start here."))).click();
    
  • 浏览器快照:

amazon_in


推荐阅读