简体   繁体   中英

SELENIUM PYTHON | Getting error when selecting an element which exists (NoSuchElementException)

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]"}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM