0

Im Using selenium and python3. I have a LIST id like to loop through in my script using nth-child(1n)

for n in self.driver_web_browser.find_element_by_css_selector('#ctl00_ContentPlaceHolder1_Estadocombo > option:nth-child({n})'):
            str.append(n.text())
            print(str)

Im getting an error please help.

for n in self.driver_web_browser.find_elements_by_css_selector('#ctl00_ContentPlaceHolder1_Estadocombo > option:nth-child({n})'):

File "/Users/ef/Desktop/MyBot/myBot/BOTS/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 614, in find_elements_by_css_selector return self.find_elements(by=By.CSS_SELECTOR, value=css_selector) File "/Users/ef/Desktop/MyBot/myBot/BOTS/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 1007, in find_elements 'value': value})['value'] or [] File "/Users/ef/Desktop/MyBot/myBot/BOTS/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/Users/ef/Desktop/MyBot/myBot/BOTS/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified (Session info: chrome=85.0.4183.102)

6
  • What kind of error? Commented Sep 13, 2020 at 8:35
  • you should be using find_elements which return the list of webelements Commented Sep 13, 2020 at 14:44
  • @Sureshmani , thanks that did help but still having an issue Commented Sep 14, 2020 at 0:00
  • @edward - Do you want to extract the text of the dropdown? Or you want to extract the text of the specific element from the dropdown? Commented Sep 14, 2020 at 6:58
  • @DilipMeghwal I am trying to extract the whole drop down list. Let me try the solution below. I had not used Select. However i found a different solution that also works .The Select method looks easier which is great! Much appreciated! Commented Sep 15, 2020 at 17:03

2 Answers 2

1

Since these are select and option values, you can use the Select class

from selenium.webdriver.support.ui import Select


data = [] 
select = Select(driver.find_element_by_id('ctl00_ContentPlaceHolder1_Estadocombo'))
print(select.options)
for opt in select.options:
    data.append(opt.text)
print(data)
Sign up to request clarification or add additional context in comments.

Comments

0

This also works for printing The whole list

basecss = '#ctl00_ContentPlaceHolder1_Estadocombo > option'
        events = self.driver_web_browser.find_elements_by_css_selector(basecss)


        for index, val in enumerate(events, 1):
            name = self.driver_web_browser.find_elements_by_css_selector("{}:nth-child({})".format( basecss,index))
            print(index,val.text)

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.