Select elements inside DIV (not select option by x)

Hi there,

I want to verify the options that are appearing in the drop down but when I use any of the keyword with options, I receive below error -
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been “select” but was “div”
How can I resolve this?

Thanks,

The statements, selectOptionByLabel, selectOptionByValue and selectOptionByIndex only work on a select tag. The error message says you are not working on a select tag, but a div. There have been many questions on this forum on this “error”/concern.

Basically, you have to treat your div “drop-down” like a web table (at least like a list of web elements), or create pathways to each of the options using parameterization, like id("myOptions")/div[${row}].

https://docs.katalon.com/katalon-studio/docs/handle_web_tables.html

Example (not actual pathway since we have no HTML to review):
List<WebElement> rowOptions = driver.findElements(By.xpath('id("myOptions")/div'));

1 Like

Hi,

Thanks for your response but I have a follow up question. Does this solution works for Boundlist as well. I checked the html code for the element and the class defined is ‘x-boundlist-item’ and I still receive the same error as mentioned above.

Regards,

Well, I don’t know since I don’t deal in “x-boundlist-item”, however, you can either add this bit of information to your subject or create a separate question with this bit of information. The more information you can provide, the better off people will be to understand your concern.

If you can provide some HTML to review, others may be able to help.

2 Likes

Hi,

Thanks for the update.
I have below HTML which can be reviewed -

Take a look at the below and see if it gives you a count of the Items

List<WebElement> roleOptions = driver.findElements(By.xpath('//li[@class="x-boundlist-item"]'))
def rows_count = roleOptions.size();

WebUI.comment("Found ${rows_count} items")
println("Found ${rows_count} items")
Or maybe

To ensure we have an unique xpath, I started at the id above the list elements, and moved down to them.

List<WebElement> roleOptions = driver.findElements(By.xpath('//div[starts-with(@id,"boundlist-") and contains(@id,"listEl")]/following-sibling::li[@class="x-boundlist-item"]'))
def rows_count = roleOptions.size();

WebUI.comment("Found ${rows_count} items")
println("Found ${rows_count} items")

Thanks for the solution, The first one worked for me but it returned value 2 instead of 3.
Is there anything related to the idnex?

if you got the count from WebUI.comment("Found ${rows_count} items"), then “hmmmm, something’s off.” but just to remind you that Lists start their count at 0, not 1. So, if you try to print the items out, it should be:

for (int cnt = 0; cnt < rows_count; cnt++) {
   WebUI.comment("item ${cnt + 1} is ${roleOptions.get(cnt).getText()}")
}

Edit:

Another idea:

If you did not get the correct count using my the class attribute, perhaps you could use the role attribute, like below.

List<WebElement> roleOptions = driver.findElements(By.xpath('//li[@role="option"]'))
def rows_count = roleOptions.size();

WebUI.comment("Found ${rows_count} items")
println("Found ${rows_count} items")

Thanks for the response. It worked for me.
How can I get the values printed along with the count?

The above should have given you the values.

To print out anything, you can use the below. To print out to the Console, use println, like:

println("your comment here ${your value here}")

or, to print out to the Log, use WebUI.comment, like:

WebUI.comment("your comment here ${your value here}")

Put your variable within curly brackets with a leading dollar sign within double quotes.

Edit: if you have multiple variables that you want to print out, each variable must be within its own curly brackets with a leading dollar sign and within the double quotes, like I did previously above:

WebUI.comment("item ${cnt + 1} is ${roleOptions.get(cnt).getText()}")

Note: this is not the only way to display text but it’s sometimes easier over all. Using plus signs gets to look messy quickly.