简体   繁体   English

向下滚动菜单并单击一个元素,然后单击错误的元素

[英]Scrolling down a menu and clicking on an element clicks on the wrong element

In my web driver test I'm scrolling down a list of menu items and clicking it. 在我的Web驱动程序测试中,我向下滚动菜单项列表并单击它。 But what I observe is that it actually navigates to a wrong link which means it has the effect of clicking some menu item at the top of the menu. 但是我观察到的是,它实际上导航到错误的链接,这意味着单击菜单顶部的某些菜单项会产生效果。 Why does this happen? 为什么会这样?

// Click menu button that launches menu.
driver.findElement(By.className("menuButton")).click();

// Scroll down the menu.
new Actions(driver)
    .moveToElement(driver.findElement(By.className("navMenu")))
    .click()
    .sendKeys(Keys.PAGE_DOWN)
    .perform();

// Find and click a menu item now visible.
new Actions(driver)        
    .moveToElement(driver.findElement(By.linkText("bottom menu item")))
    .click()
    .perform();

Be sure there is no another bottom menu item start with same "bottom menu item" text and placed previous to your element. 确保没有其他底部菜单项以相同的“底部菜单项”文本开头并放置在元素之前。

You can also try to scroll using Javascript and scrollIntoView method, if click not working by itself: 如果单击本身scrollIntoView ,您也可以尝试使用Javascript和scrollIntoView方法滚动:

JavascriptExecutor js = (JavascriptExecutor) driver;

// Click menu button that launches menu.
driver.findElement(By.className("menuButton")).click();

driver.findElement(By.className("navMenu")).click();

// Find and click a menu item now visible.
WebElement bottomMenuItem = driver.findElement(By.linkText("bottom menu item"));
js.executeScript("arguments[0].scrollIntoView(true)", bottomMenuItem);
bottomMenuItem.click();

Step 1. Click menu button that launches menu. 步骤1.单击菜单按钮,启动菜单。

driver.findElement(By.className("menuButton")).click();
driver.findElement(By.className("navMenu")).click();

Step 2. 第2步。

WebElement bottomMenuItem = driver.findElement(By.linkText("bottom menu item"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click()", bottomMenuItem);

Note - There could be some micro seconds delay before "bottom menu item". 注意-“底部菜单项”之前可能会有微秒的延迟。 So there could be chances at some point it could fail clicking on "bottom menu item". 因此,在某些时候可能无法单击“底部菜单项”。 So you may would need to wait for a second to make this action more reliable without facing failure in future. 因此,您可能需要等待一秒钟才能使此操作更可靠,而不会在将来遇到失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM