Python selenium下拉菜单cli

2024-06-01 09:55:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从下拉菜单中选择选项,为此我使用:

br.find_element_by_xpath("//*[@id='adyen-encrypted-form']/fieldset/div[3]/div[2]/div/div/div/div/div[2]/div/ul/li[5]/span").click()

选择第4个月选项,但当我选择Pyton时,返回错误消息:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible (Session info: chrome=51.0.2704.103) (Driver info: chromedriver=2.22.397929 (fb72fb249a903a0b1041ea71eb4c8b3fa0d9be5a),platform=Mac OS X 10.11.5 x86_64)

这是HTML代码:

^{pr2}$

怎么了?我知道selenium找不到元素,但我不知道为什么,xpath错了?我需要用其他方法来找到元素?谢谢你的帮助


Tags: brdivforminfoid元素by选项
1条回答
网友
1楼 · 发布于 2024-06-01 09:55:54

您应该使用Select()从下拉列表中选择一个选项,如下所示:

from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

 wait = WebDriverWait(driver, 10)


 element = wait.until(EC.visibility_of_element_located((By.ID, "dwfrm_adyenencrypted_expiryMonth")))

 select = Select(element)
 select.select_by_value("04")

已编辑:-如果不幸的是上面的不起作用,您也可以尝试使用.execute_script(),如下所示:-

^{pr2}$

希望它能起作用…:)

相关问题 更多 >