How to activate checkbox inside a Dropdown with Python Selenium

Viewed 112

How can I check the checkbox inside a dropdown menu with Python Selenium?

Within a list I have many checkboxes. I would like to activate the checkboxes that I pass through the code, but I have not been able to achieve it.

This is the structure of the web page, I can't see the selector Id for the checkbox, only I can see it the data-value or the text value name Texto1, Texto2, Texto3, etc.

Image

So my question is how can I check the checkbox with the data-value or text name?

I'm using this code:

from selenium import webdriver
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome('C:/Driver/chromedriver.exe')
driver.implicitly_wait(30)
driver.set_page_load_timeout(50)
driver.get('http://midominio.com/')
driver.implicitly_wait(40)
driver.find_element_by_id("dropdownMenu2").click()
2 Answers

To click first checkbox use:

driver.find_element_by_css_selector("#Plants00>li:nth-of-type(1)>a>input").click()

Second:

driver.find_element_by_css_selector("#Plants00>li:nth-of-type(2)>a>input").click()

Also remove duplication of implicitly_wait. YOu do not really need it.

You can use xpath like //*[text()='Texto1'] to select the checkbox

Related