1

I am trying to fill a form up. here is the html for the drop down list

<div class="mb-3">
            <label for="cityId" class="form-label FormLabelsEn">City</label> <label for="cityId" class="form-label float-end">المدينة</label> <select class="form-select citySelect" aria-label="Default select example" id="cityId" required="required"><option value="" selected="" disabled="">اختر من القائمة</option><option value="Ahssa - الأحساء"> Ahssa - الأحساء</option><option value="Ar Rass - الرس"> Ar Rass - الرس</option><option value="Buraidah - بريدة"> Buraidah - بريدة</option><option value="Dammam - الدمام"> Dammam - الدمام</option><option value="Dhahran - الظهران"> Dhahran - الظهران</option><option value="Hafr Albatin - حفر الباطن"> Hafr Albatin - حفر الباطن</option><option value="Jubail - الجبيل"> Jubail - الجبيل</option><option value="KAUST - جامعة الملك عبدالله للعلوم والتقنية"> KAUST - جامعة الملك عبدالله للعلوم والتقنية</option><option value="Khobar - الخبر"> Khobar - الخبر</option><option value="Madina - المدينة المنورة"> Madina - المدينة المنورة</option><option value="Rabigh - رابغ"> Rabigh - رابغ</option><option value="Riyadh - الرياض"> Riyadh - الرياض</option><option value="Tabuk - تبوك"> Tabuk - تبوك</option><option value="Umluj - أملج"> Umluj - أملج</option><option value="Unaizah - عنيزة"> Unaizah - عنيزة</option></select>
        </div>

i am using the below code to click on Riyadh from the drop down list. But I am getting an error

wait = WebDriverWait(driver, 10)

dropdown = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/form/div[5]/select")))
ActionChains(driver).move_to_element(dropdown).perform()
wait
city = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/form/div[5]/select/option[5]")))
ActionChains(driver).move_to_element(city).click().perform()

I am getting the following error:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds

Can someone please help me with how to click on the drop down.

1 Answer 1

1

You can use select class to select an element from drop-down. Like below,

select = Select(driver.find_element_by_id('cityId'))
# select by index
select.select_by_index(1)

import:

from selenium.webdriver.support.ui import Select
Sign up to request clarification or add additional context in comments.

5 Comments

I did the same and am getting this error raise NoSuchElementException("Could not locate element with visible text: %s" % text)
select = Select(driver.find_element_by_id('cityId')) # select by visible text select.select_by_index(4) this works, If you dont mind can you change the answer accordingly and I will select it as the answer. thanks a lot
oh one more thing. if you can help me with the check box. i am using this code to click on it driver.find_element_by_xpath("/html/body/div[1]/form/div[6]/input").click(); Im getting this error on the check box raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (477, 1292)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/form/div[6]/input"))).click()
You can try other ways to fix this as well. Please refer the question stackoverflow.com/questions/48665001/…

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.