如何在 Java Selenium WebDriver 主窗口的特定菜单中滚动鼠标 [英] How can I scroll mouse in specific menu in main window in Java Selenium WebDriver

查看:61
本文介绍了如何在 Java Selenium WebDriver 主窗口的特定菜单中滚动鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Selenium WebDriver 的帮助下在 Chrome 中找到一个网络元素.当驱动程序启动 URL 时,将显示一个项目列表,驱动程序必须从该列表中选择一个特定的项目.

I am trying to find a web element in Chrome with help of Selenium WebDriver. When the driver launches the URL, a list of projects is being displayed and the driver has to select a specific project from that list.

只要该项目在列表的顶部,就可以并且可以找到该项目,但是如果项目位于列表的最底部(根据列表已按字母顺序排列并说尝试了列表中的第 57 条记录被选中),测试一直失败,驱动找不到web元素!!!

As long as that project is on top of the list, it's ok and can find the project, however if project is at the very bottom of the list (as per list has been ordered alphabetically and say record 57 from list is tried to be selected), test keeps failing and driver can't find the web element!!!

我终于到了这一点,我需要滚动我的列表直到该项目出现,但根据这个滚动条在那个菜单中而不是在主窗口中,这个命令甚至没有执行!我是否需要为驱动程序识别项目菜单?如何在窗口中向下滚动该项目菜单?我不想滚动主 Web 窗口,我只需要在项目列表中滚动.

I finally got to this point that I need to scroll my list till that item shows up, but as per this scroll bar is in that menu not in the main window this command is not even executed! Do I need to identify the project menu to driver at all? how can I scroll down that project menu in the window? I don't want to scroll the main Web window, I need to scroll in the project list only.

我尝试了所有可能的解决方案,并在 Stack Overflow 论坛和互联网上冲浪,但无法修复此错误.如果你们看看下面的这段代码并给我一些建议,那就太好了.如果我必须提供更多信息,请告诉我.很高兴在这里提到我正在从电子表格中读取projectName".

I tried all the possible solutions and was surfing all over the Stack Overflow forum as well as internet but couldn't fix this error. It would be great if you guys have a look at this code bellow and give me some advice. Please let me know if I have to provide more information. Good to mention here that I am reading the "projectName" from spreadsheet.

// Initially I need to hover the mouse on Select Project menu.

Actions action = new Actions(driver);
WebElement list = driver.findElement(By.xpath("//*[@id=\"gridview-1032\"]")); 
action.moveToElement(list);
JavascriptExecutor js = (JavascriptExecutor) driver;

 // Now I need to scroll down till find my desire project in the list.

 WebElement Project = driver.findElement(By.xpath("//*[text()= '"+ projectName +"']"));                 
js.executeScript("arguments[0].scrollIntoView(true);",Project);
Project.click();

实际结果:

线程main"org.openqa.selenium.NoSuchElementException 中的异常:没有这样的元素:无法找到元素:{"method":"xpath","selector":"///*[text()='projectName']"}

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[text()= 'projectName']"}

预期结果:找到列表中的元素并单击该项目以启动项目!

Expected result: Find the element in the list and click on that item to launch the project!

推荐答案

下面是对我有用的代码.这段代码在你的情况下工作正常

Here below is the code that worked for me.This code works fine in your case

Actions action = new Actions(driver);
WebElement list = driver.findElement(By.xpath("//*[@id=\"gridview-1032\"]")); 
action.moveToElement(list);
JavascriptExecutor js = (JavascriptExecutor) driver;

 // Now I need to scroll down till find my desire project in the list.

 WebElement Project = driver.findElement(By.xpath("//*[text()= '"+ projectName +"']"));                 
js.executeScript("arguments[0].click();",Project);

这篇关于如何在 Java Selenium WebDriver 主窗口的特定菜单中滚动鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆