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.

raptorzee
  • 151
  • 1
  • 11

1 Answers1

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Nandan A
  • 2,702
  • 1
  • 12
  • 23
  • I did the same and am getting this error raise NoSuchElementException("Could not locate element with visible text: %s" % text) – raptorzee Nov 30 '21 at 07:52
  • 1
    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 – raptorzee Nov 30 '21 at 07:56
  • 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) – raptorzee Nov 30 '21 at 08:00
  • `WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/form/div[6]/input"))).click()` – Nandan A Nov 30 '21 at 08:07
  • You can try other ways to fix this as well. Please refer the question https://stackoverflow.com/questions/48665001/can-not-click-on-a-element-elementclickinterceptedexception-in-splinter-selen – Nandan A Nov 30 '21 at 08:08