0

I am a python newbie having an issue scraping the the text from a drop down table

here is where the issue is:

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.get("https://www.ehail.ca/quotes/?1590984761655")

desired_data =
Select(driver.find_element_by_xpath("/html/body/div[1]/div[2]/form[2]/table[1]/tbody/tr/td/table/tbody[1]/tr[2]/td[10]/select"))

desired_data.select_by_index(1)

print(desired_data.text))

and get the resulting error:

AttributeError: 'Select' object has no attribute 'get_attribute' 

I have also tried print(desired_data.text) but had no luck there.

Can I do this with selenium or will I need another library like beautiful soup?

Thank you for reading

edit1: I have filled in the required fields and still get same error. I am able to select the element but when try to print I get <selenium.webdriver.support.select.Select object at 0x087810B8>

2 Answers2

0

You are seeing the correct behavior.

In your program, just after invoking get(url) as you are invoking the the Select(webelement) Class on the desired WebElement without filling out the previous mandatory fields, the <option> items doesn't gets populated/created:

no_options

Hence forcefully when you invoke select_by_index(1) method, which in turn internally calls if opt.get_attribute("index") == match: raises the error:

AttributeError: 'Select' object has no attribute 'get_attribute' 

Implementation of select_by_index():

def select_by_index(self, index):
    """Select the option at the given index. This is done by examing the "index" attribute of an
       element, and not merely by counting.

       :Args:
        - index - The option at this index will be selected

       throws NoSuchElementException If there is no option with specisied index in SELECT
       """
    match = str(index)
    for opt in self.options:
        if opt.get_attribute("index") == match:
            self._setSelected(opt)
            return
    raise NoSuchElementException("Could not locate element with index %d" % index)

Solution

Filling up the previous mandatory fields, the <option> elements will get populated and the program execution will succeed.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Call .first_selected_option method first before use .text method.

Refers to the documentation:

The first selected option in this select tag (or the currently selected option in a normal select)

desired_data.select_by_index(1)
print(desired_data.first_selected_option.text)

It will return the current selected option.

frianH
  • 7,295
  • 6
  • 20
  • 45