Home > Back-end >  SELENIUM PYTHON | Getting error when selecting an element which exists (NoSuchElementException)
SELENIUM PYTHON | Getting error when selecting an element which exists (NoSuchElementException)

Time:12-18

https://huggingface.co/spaces/BatuhanYilmaz/Whisper-Auto-Subtitled-Video-Generator Trying to select an option from the drop down menu

What I tried:

driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div/div/div/section[2]/div/div[1]/div/div[2]/div/div/div/div[1]').click()

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[1]/div[1]/div/div/div/section[2]/div/div[1]/div/div[2]/div/div/div/div[1]"}

CodePudding user response:

So while your xpath is correct. The initial URL that you gave is generating the webpage that you want to interact with inside of an iframe. You could follow the advice from here.

However since we know the link that the iframe is generating the source with you can just load the source page, which is:

https://batuhanyilmaz-whisper-auto-subtitled-video-gen-ac3cac8.hf.space/?__theme=light

instead of loading the original link that you had included in your question.

CodePudding user response:

The dropdown is inside an iframe, you have to switch to that iframe then you need to select an option from the dropdown, try this code:

iframe = WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, ".//iframe[@title='Space app']")))

driver.find_element(By.XPATH, "(.//div[@data-baseweb='select']/div/div)[1]").click()
options = WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located((By.XPATH, ".//div[@data-baseweb='popover']//ul//li/span")))
print("Total options:", len(options))

option_to_select = "medium"

i = 0
for option in range(len(options)):
    option_text = driver.find_element(By.XPATH, "(.//div[@data-baseweb='popover']//ul//li/span)["   str(i   1)  "]").text
    print(option_text)
    if option_to_select == option_text:
        driver.find_element(By.XPATH, "(.//div[@data-baseweb='popover']//ul//li/span)["   str(i   1)   "]").click()
        break
    i  = 1
  • Related