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

心不动则不痛 提交于 2020-01-05 03:57:09

问题


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.

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!!!

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.

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

Actual result:

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!


回答1:


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



回答2:


I found an alternative solution for this question which is simpler:

// Create instance of Javascript executor

JavascriptExecutor je = (JavascriptExecutor) driver;

//Identify the WebElement which will appear after scrolling down

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



回答3:


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

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


来源:https://stackoverflow.com/questions/54002523/how-can-i-scroll-mouse-in-specific-menu-in-main-window-in-java-selenium-webdrive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!