1

We have a drop down list written using div tags. Selenium fails to identify the elements which are not in the display and throws an error.Using JS code to scroll - .scrollBy(0,y) option wouldn't work here.

The drop-down list is not written using the select tag. It is more like a list. The list is first displayed as a picker button. When it is clicked it displays a list which can be scrolled. The list is written using div tags.

<div id="xxx_pickerMenuItem1" type = "button">
    <div id = "xxx_pickerMenuItem1_text">Text1</div>
<div>
<div id="xxx_pickerMenuItem2" type = "button">
    <div id = "xxx_pickerMenuItem2_text">Text2</div>
<div>
.
.
.
.

Is there a way to scroll the list down and check whether the element required is displayed or not?

2 Answers 2

1

Just wrap your WebElement into Select Object as shown below

Select dropdown = new Select(driver.findElement(By.id("identifier")));

Once this is done you can select the required value in 3 ways. Consider an HTML file like this

<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>

Now to identify dropdown do

Select dropdown = new Select(driver.findElement(By.id("designation")));

To select its option say 'Programmer' you can do

dropdown.selectByVisibleText("Programmer ");

or

dropdown.selectByIndex(1);

or

dropdown.selectByValue("prog");

if the value is not present you will get an WebDriverException !

Edit: The dropdown is composed using div's

driver.findElement(By.name("applicantContact.areaOfOd")).click()
driver.findElement(By.name("applicantContact.areaOfOd")).sendKeys(
                            Keys.ARROW_DOWN);
driver.findElement(By.name("applicantContact.areaOfOd")).sendKeys(
                            Keys.ARROW_DOWN);
driver.findElement(By.name("applicantContact.areaOfOd")).sendKeys(Keys.ENTER);

Above code does click on element and then press Arrow down twice and then press Enter. Hope this help you frame your solution.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the response. The drop-down list is not written using the select tag. It is more like a list. The list is first displayed as a picker button. When it is clicked it displays a list which can be scrolled. The list is written using div tags. <div id="xxx_pickerMenuItem1" ...> <div id = "xxx_pickerMenuItem1_text">Text1</div> <div> <div id="xxx_pickerMenuItem2" ...> <div id = "xxx_pickerMenuItem2_text">Text2</div> <div> . . . .
Unfortunately, it doesn't respond to the arrow keys when i try it manually. So, i guess it wouldn't respond the above code.The only way to make the options visible is to scroll down using the mouse.
We can scroll the dropdownlist using the mouse scroll and then click on the desired option
Why dont you write the code to do the exact same thing ? Whats the problem ?
I don't know the code to scroll the list down. window.scrollBy(x,y) or element.scrollBy(x,y) wouldn't work
0

Try figuring out how to select without using the arrow or scroll button. Often times, you can click the button then start typing to quickly select the correct item in the list. In selenium, that would be these steps...

  1. Click picker button
  2. Select the div list and use "SendKeys" and type the exact text as it appears. You can send a "\n" if you need to click enter to select the item you wanted. Also you may need to do a focus event on it first.
  3. Now that the item has been selected, you can simply get the .Text of the item that was selected.

Alternatively, you can

  1. Click picker button
  2. Select the element that holds the list and use the (string allOptions = myElement.Text;)
  3. see if it's in the list (bool textFound = allOptions.Contains("what I'm looking for");)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.