selenium 蟒|选择已存在的元素时出错(NoSuchElementException)

jxct1oxe  于 2022-12-18  发布在  其他
关注(0)|答案(2)|浏览(131)

https://huggingface.co/spaces/BatuhanYilmaz/Whisper-Auto-Subtitled-Video-Generator尝试从下拉菜单中选择选项
我尝试了:

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

9q78igpj1#

因此,虽然您的xpath是正确的,但您给出的初始URL正在生成您希望在iframe内部与之交互的网页,您可以遵循here中的建议。
然而,由于我们知道iframe生成源代码的链接,您可以只加载源代码页面,即:

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

而不是加载问题中包含的原始链接。

dgiusagp

dgiusagp2#

下拉列表在一个iframe中,你必须切换到该iframe,然后你需要从下拉列表中选择一个选项,尝试以下代码:

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

相关问题