简体   繁体   English

如何在Java Selenium WebDriver的主窗口中的特定菜单中滚动鼠标

[英]How can I scroll mouse in specific menu in main window in Java Selenium WebDriver

I am trying to find a web element in Chrome with help of Selenium WebDriver. 我正在尝试借助Selenium WebDriver在Chrome中找到一个Web元素。 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. 驱动程序启动URL时,将显示项目列表,并且驱动程序必须从该列表中选择特定项目。

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!!! 只要该项目在列表的顶部,就可以找到该项目,但是,如果项目在列表的底部(根据列表的字母顺序排列,并说试图从列表中选择记录57,则可以选择该项目) ),测试不断失败,驱动程序找不到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. 我不想滚动主Web窗口,只需要滚动项目列表即可。

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. 我尝试了所有可能的解决方案,并在整个Stack Overflow论坛和Internet上冲浪,但无法修复此错误。 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. 值得一提的是,我正在从电子表格中读取“ projectName”。

// 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();

Actual result: 实际结果:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[text()= 'projectName']"} 线程“主”中的异常org.openqa.selenium.NoSuchElementException:否这样的元素:无法找到元素:{“ 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);

I found an alternative solution for this question which is simpler: 我为这个问题找到了一个更简单的替代解决方案:

// Create instance of Javascript executor //创建Javascript执行程序的实例

JavascriptExecutor je = (JavascriptExecutor) driver;

//Identify the WebElement which will appear after scrolling down //确定向下滚动后将显示的WebElement

WebElement Project = driver.findElement(By.className("x-grid-item-container"));

// now execute query which actually will scroll until that element is not appeared on page. //现在执行查询,该查询实际上将滚动直到该元素没有出现在页面上。

je.executeScript("arguments[0].scrollIntoView(true);",Project);      

//Login to desired project //登录到所需项目

Project.click();

I thought this might be an issue for someone else, I am adding my solution as well, it might be helpful: 我以为这对其他人可能是个问题,我也在添加解决方案,这可能会有所帮助:

//it find the list and scroll 3000 pixel //找到列表并滚动3000像素

EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(driver6);
eventFiringWebDriver.executeScript("document.querySelector('#gridview-1032').scrollTop=3000");

//find the project and login //找到项目并登录

WebElement Project = driver6.findElement(By.xpath("//*[text()= '"+ projectName +"']"));
Project.click();

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

相关问题 如何使用 Selenium WebDriver 和 Java 关闭特定的 window? - How can I close a specific window using Selenium WebDriver with Java? 使用Selenium WebDriver Java将鼠标悬停在主菜单上时不显示子菜单 - Not displaying submenu when mouse hover on main menu using selenium webdriver java 如何使用带有 Java 的 Selenium WebDriver 滚动特定的 DIV? - How to scroll a specific DIV using Selenium WebDriver with Java? 滚动到特定元素Selenium WebDriver Java - Scroll to specific element Selenium WebDriver Java 如何使用Selenium WebDriver for Java和PageFactory执行鼠标移动 - How can i perform mouse move using Selenium WebDriver for Java and PageFactory 如何在 selenium webdriver 的下拉窗口中向下滚动? - How to scroll down in dropdown window in selenium webdriver? 如何将Selenium WebDriver中的页面滚动到特定链接 - How to scroll the page in selenium webdriver to specific link 如何使用 Selenium WebDriver 和/或 Java 中的 Sikuli 平滑滚动 - How to scroll smoothly with Selenium WebDriver and/or Sikuli in Java 如何在 Java 中使用 Selenium WebDriver 向下滚动 - How to scroll down using Selenium WebDriver with Java 如何验证特定文本下的文本 <DIV> 使用Selenium WebDriver(java)和Chrome浏览器标记? - How can I verify presence of text under a specific <DIV> tag using Selenium WebDriver (java) and Chrome?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM