【发布时间】:2023-02-02 20:04:20
【问题描述】:
我试图选择不同的日期而不是默认日期(当前日期)。例如,初始页面弹出持股日期:2023/02/01,但我想从下拉菜单中选择不同的日期,比如 2022/12/23。我的环境是:Selenium 4.3.0 和 Python 3.9.7,Chrome
以下是我的代码:
url = "https://www3.hkexnews.hk/sdw/search/mutualmarket.aspx?t=hk&t=hk&t=hk&t=hk"
driver = webdriver.Chrome()
driver.get(url)
select_element = driver.find_element(By.XPATH, "//input[@name='txtShareholdingDate']").click()
# The above pop up the required page with Date dropdown, tried different code to select the date but failed. My codes are:
action = ActionChains(select_element)
action.send_keys("2023",Keys.ARROW_DOWN)
action.send_keys("1",Keys.ARROW_DOWN)
action.send_keys("31",Keys.ARROW_DOWN)
action.send_keys(Keys.ENTER)
action.perform()
# AttributeError: 'NoneType' object has no attribute 'execute'
# Also tried
select = driver.find_element(By.ID, "txtShareholdingDate")
select.select_by_value("2023/01/31")
driver.find_element(By.ID, 'btnSearch').click()
# AttributeError: 'WebElement' object has no attribute 'select_by_value'
# Any suggestions ?
【问题讨论】:
-
对于错误 - “AttributeError: 'WebElement' object has no attribute 'select_by_value'” - 你必须修改行 - select = Select(driver.find_element(By.ID, "txtShareholdingDate")) ,并添加导入 -从 selenium.webdriver.support.select 导入选择