selenium - 试图将鼠标悬停在菜单上并单击子菜单中的链接,但没有运气
问题描述
我正在学习将鼠标悬停在菜单上,然后单击子菜单中的链接。
该方案是转到 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();
解决方案
要访问 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();
浏览器快照:
推荐阅读
- javascript - getElementById 到数组到函数来切换
- jenkins - Jenkins 脚本化管道可以禁用并发构建吗?
- ruby-on-rails - Ruby-On-Rails:Spree - 客户添加订单备注
- c++ - 为什么我会收到“从 const 指针到指针的无效转换?
- identityserver4 - 刷新 IdentityServer4 的令牌
- basic - 为什么 BASIC 将 0x1a 附加到文件末尾?
- javascript - 在 Nuxt SPA 中运行 Vue 代码时出现问题(同一元素上的 vmodel 冲突)
- excel - Excel:将联系信息组织成类似数据的列
- java - 如何使用一系列的while循环来向用户提问?
- nginx - 设置默认 nginx 位置