TransWikia.com

Find the number of items in a dropdown menu using python in Selenium

Stack Overflow Asked by user3794200 on December 3, 2021

I am writing a script to count the number of options in the dropdown menu on the webpage – https://docs.google.com/forms/d/e/1FAIpQLSeI8_vYyaJgM7SJM4Y9AWfLq-tglWZh6yt7bEXEOJr_L-hV1A/viewform?formkey=dGx0b1ZrTnoyZDgtYXItMWVBdVlQQWc6MQ

I understood the concept if the dropdown is in select class- saw so many videos and examples for that, but since it is in div class, I am unable to figure out how to do that, here is what I got so far, which throws me an error.

driver.find_element_by_css_selector("div[role=presentation]").click()
dr=driver.find_elements_by_css_selector("div[role=option]")

print(len(dr))
for iterate in dr:
    print(iterate)

The answer shows 4, when it should be 3 and the options are not printed correctly.

4
<selenium.webdriver.remote.webelement.WebElement (session="315b0a46b5fb15dbde9e2b2623204de1", element="aa3a69a2-f35a-469a-928d-b3e82daacb7b")>
<selenium.webdriver.remote.webelement.WebElement (session="315b0a46b5fb15dbde9e2b2623204de1", element="6d7a55f4-f894-4bbf-bb63-48d156d7b1c2")>
<selenium.webdriver.remote.webelement.WebElement (session="315b0a46b5fb15dbde9e2b2623204de1", element="e0fa0747-5488-4f77-bd7d-c516c0c83731")>
<selenium.webdriver.remote.webelement.WebElement (session="315b0a46b5fb15dbde9e2b2623204de1", element="bfbb8e26-dcde-4cbc-b145-2a8598d77dcb")>

I am confused where I went wrong. Thanks in advance . You guys are the best 🙂

3 Answers

There are 4 elements with your selector div[role=presentation], but there is a different one. The default option has the class name isPlaceholder, so make an exception to your selector by adding :not([class*='isPlaceholder'])

driver.find_element_by_css_selector("div[role=presentation]").click()

#may you need to wait a minute here

dr=driver.find_elements_by_css_selector("div[role=option]:not([class*='isPlaceholder'])")

print(len(dr))
for iterate in dr:
    print(iterate.text)

And use .text to grab text.

Answered by frianH on December 3, 2021

There are 4 options with div[role=option], the first one is the default "Choose" value, and the other 3 are Choice 1, 2 and 3

If you want to print the options properly, you should get the span attribute of each option and print it as iterate.text

Answered by ArgiesDario on December 3, 2021

This is one of worst practice in selenium automation

For multiple reasons, logging into sites like Gmail and Facebook using WebDriver is not recommended. Aside from being against the usage terms for these sites (where you risk having the account shut down), it is slow and unreliable.

The ideal practice is to use the APIs that email providers offer, or in the case of Facebook the developer tools service which exposes an API for creating test accounts, friends and so forth. Although using an API might seem like a bit of extra hard work, you will be paid back in speed, reliability, and stability. The API is also unlikely to change, whereas webpages and HTML locators change often and require you to update your test framework.

Logging in to third party sites using WebDriver at any point of your test increases the risk of your test failing because it makes your test longer. A general rule of thumb is that longer tests are more fragile and unreliable.

Please refer selenium websites

Answered by Justin Lambert on December 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP