1

I am trying to select South Africa from the option list below:

<select name="from_country" id="from_country" data-role="none" class="button-negative button-negative-country-select classic" >
    <option value="ZA">South Africa</option>
    <option value="ZW" selected="selected">Zimbabwe</option>
    <option value="BW">Botswana</option>
    <option value="ZM">Zambia</option>
    <option value="MW">Malawi</option>
</select>

I have tried using xpath like the solotion here :

driver.find_element_by_xpath("//select[@name='from_country']/option[text()='South Africa']").click()

but this results in:

Exception has occurred: NoSuchElementException

I have then assumed that maybe the form is been rendered after the page has finished loading and I applied WebDriverWait:

WebDriverWait(driver,20).until(EC.element_located_to_be_selected((By.XPATH,"//select[@name='from_country']/option[text()='South Africa']"))).click()

and it results in:

Exception has occurred: TimeoutException

If I use Chrome developer tools and search for the element using xpath it shows that the xpath is valid.

The webpage in question: https://www.mukuru.com/sa/send-money-to-nigeria/

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
tendaitakas
  • 192
  • 1
  • 17

2 Answers2

1

Your element target inside a frame:

<iframe src="https://mobile.mukuru.com/mobi/pricecheck?country_shortcode=ZA&amp;iframe=1" class="homeIframe" name="calculatorFrame" scrolling="no" marginheight="0px" marginwidth="0px" allowfullscreen="" width="340px" height="430px" frameborder="1">
    #document
        ....
        ....
        YOUR TARGET HERE

You need switch it first, recommendations using .frame_to_be_available_and_switch_to_it.

To select dropdown you can use Select class, with .select_by_visible_text('...') method:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

driver.get('https://www.mukuru.com/sa/send-money-to-nigeria/')
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, 'iframe.homeIframe')))
drop_down = Select(wait.until(EC.element_to_be_clickable((By.ID, 'from_country'))))
drop_down.select_by_visible_text('South Africa')
frianH
  • 5,901
  • 6
  • 13
  • 36
-1

To select South Africa from the options listed you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using ID and select_by_visible_text():

    select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "from_country"))))
    select.select_by_visible_text("South Africa")
    
  • Using CSS_SELECTOR and select_by_value():

    select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#from_country[name='from_country']"))))
    select.select_by_value("ZA")
    
  • Using XPATH and select_by_visible_text():

    select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='from_country' and @name='from_country']"))))
    select.select_by_visible_text("South Africa")
    
  • 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
    

References

You can find a couple of relevant detailed discussions in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217