0

Im really struggling with selecting an item from a drop down menu on a site I'm trying to scrape. The code for the HTML is shown below:

<select id="averagesMP" name="MP" onchange="reQuery(this);" style="width: 150px">
       <option value="" selected="selected"></option>
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
</select>

I've tried everything I can think of and find on StackOverflow! I've noticed that this seems to work to select the actual drop down box:

select_mp = Select(driver.find_element_by_xpath("//select[@name='MP']")) 

but I only assume that works because the code runs and doesn't give any errors. However, I still can't select anything from the dropdown menu. I've tried:

  • find_element_by_xpath/name/id/tag/value/text
  • select_mp.select_by_value('2')
  • select_mp.select_by_visible_text('2')
  • select_mp.select_by_index(2)
  • driver.find_element_by_xpath("//select[@name='MP']/option[@value='4']").click()
  • driver.find_element_by_xpath("//option[@value='4']").click()
  • driver.find_element_by_id('averagesMP').click()

Does anyone have any advice? I know that this question seems to get asked alot on here, but I've tried everything I've seen in a post and its not working! It doesn't seem like a very complicated dropdown menu, only very elusive.

Some helpful info:

  • I'm using Safari and SafariDriver

  • I keep getting an error message of:

    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: Message: 
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To select the <option> with text as 2 using Selenium you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and select_by_visible_text():

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#averagesMP[@name='MP']"))))
    select.select_by_visible_text('2')
    
  • Using XPATH and select_by_value() in a single line:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='averagesMP' and @name='MP']")))).select_by_value('2')
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import Select
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for getting back to me! It looks like using XPATH is trying to access it, but now I'm getting the following error: Message: Element –  Aug 22 '20 at 14:45