list item li is not selecting the item from the drop down

Hi,

I need to select a item in the drop down box. This drop down box works as ul and li items. Pls. refer the attachments.[

img]http://forumsqa.com/wp-content/uploads/2015/05/Inspect-Element.png[/img]

While I run the below code to select the item, selenium webdriver returns element is not visible to select.

WebElement statusUl = driver.findElement(By.xpath(“//*[@id=’ddlCreateStatus-” + strProjId + “_listbox’]”));
statusUl.click();
Thread.sleep(3000);
List<WebElement> oStatusLis = statusUl.findElements(By.tagName(“li”));

for(WebElement oStatusLi: oStatusLis){
if(oStatusLi.getAttribute(“innerHTML”)==”Paused”)
{
oStatusLi.click();
break;
}
}

I appreciate if any one give me an idea to select this item.

list item li is not selecting the item from the drop down
list item li is not selecting the item from the drop down
on May 20, 2015.
Add Comment
1 Answer(s)

The reason why your are getting this error is because the element is actually hidden. You have to hover over the drop down for it to be visible but in you test you are simply clicking.

You can solve this problem by using the Action class. On http://toolsqa.com, there is an element called tutorials in the top nav bar. This element shows a similar problem.

The code for it is as mentioned below, you can make some changes to the code to suit your need


FirefoxDriver idriver = new FirefoxDriver();
idriver.get("http://www.toolsqa.com");
WebElement element = idriver.findElement(By.xpath("//*[@id='main-nav']/li[2]/a"));
Actions actions = new Actions(idriver);
actions.moveToElement(element);
actions.build().perform();
Thread.sleep(1000);
idriver.findElement(By.xpath("//*[@id='main-nav']/li[2]/ul/li[1]/a")).click();
Virender Singh Professor Answered on May 21, 2015.

Virender,

Thanks,Appreciate your suggestion. Mouse hover doesn’t  hover on the list item, it just closed the drop down box when  actions.build().perform() happened, when the movetoElement is applied to ul item and the li element click is saying element is not visible.

But if I apply movetoElement to li item, It says the console error message as org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Offset within element cannot be scrolled into view: (0, 0):

so I took the x, y location and applied the below code, but still it returns the same offset error.

List<WebElement> oStatusLis = statusUl.findElements(By.tagName("li"));
for(WebElement oStatusLi: oStatusLis){

//if(oStatusLi.getAttribute("innerHTML")=="Paused")

//{

//oStatusLi.click();

System.out.println(" Inner HTML= " + oStatusLi.getAttribute("innerHTML"));

System.out.println(" Tag Name= " + oStatusLi.getTagName());

System.out.println(" Get Text= " + oStatusLi.getText());

System.out.println(" Is Displayed= " + oStatusLi.isDisplayed());

System.out.println("Class= " + oStatusLi.getClass());

System.out.println("Is Selected = " + oStatusLi.isSelected());

int ylocation = oStatusLi.getLocation().y;

int xlocation = oStatusLi.getLocation().x;

System.out.println("x location:" + xlocation + " y location:" + ylocation);

action.moveToElement(oStatusLi, xlocation, ylocation).build().perform();

//break;

//}

}

Error messages
Exception in thread “main” org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Offset within element cannot be scrolled into view: (0, 0): [object HTMLLIElement]
Command duration or timeout: 70 milliseconds
Build info: version: ‘2.45.0’, revision: ’32a636c’, time: ‘2015-03-05 22:01:35′
System info: host: ‘Dell03′, ip: ‘10.112.8.76’, os.name: ‘Windows 7′, os.arch: ‘amd64′, os.version: ‘6.1’, java.version: ‘1.8.0_45′
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=38.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 33ee5eed-3dc2-478a-9f1e-bb8b99e74c0e
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteExecuteMethod.execute(RemoteExecuteMethod.java:34)
at org.openqa.selenium.remote.RemoteMouse.mouseMove(RemoteMouse.java:96)
at org.openqa.selenium.interactions.MoveToOffsetAction.perform(MoveToOffsetAction.java:37)
at org.openqa.selenium.interactions.CompositeAction.perform(CompositeAction.java:31)
at TstPackage.TstClass.main(TstClass.java:253)
Caused by: org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Offset within element cannot be scrolled into view: (0, 0): [object HTMLLIElement]
Build info: version: ‘2.45.0’, revision: ’32a636c’, time: ‘2015-03-05 22:01:35′
System info: host: ‘Dell03′, ip: ‘10.112.8.76’, os.name: ‘Windows 7′, os.arch: ‘amd64′, os.version: ‘6.1’, java.version: ‘1.8.0_45′
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.mouseMoveTo(file:///C:/Users/Admin/AppData/Local/Temp/anonymous3888954283682698584webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10581)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Admin/AppData/Local/Temp/anonymous3888954283682698584webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/Admin/AppData/Local/Temp/anonymous3888954283682698584webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/Admin/AppData/Local/Temp/anonymous3888954283682698584webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)

thanks,
Sudhakar

on May 21, 2015.

Try sending mouse hover to an element just above the ul element or to the parent element which is accepting the mouseHover event. Its will be a hit and trial process, with each try move to the parent of the element.

virender

on May 21, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.