使用Selenium python从下拉菜单中选择页码

bfnvny8b  于 2022-12-23  发布在  Python
关注(0)|答案(1)|浏览(192)

我正在尝试将页码从https://stats.oecd.org/Index.aspx?DataSetCode=REVDEU上的默认页码1更改为页码2
目前,我的代码打开页面,似乎什么也不做,最终超时。

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

driver = webdriver.Chrome()
driver.get("https://stats.oecd.org/Index.aspx?DataSetCode=REVDEU")

select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
    (By.XPATH, "//select[starts-with(@id, 'PAGE')][starts-with(@name,'PAGE')]"))))
select.select_by_visible_text('2')
hlswsv35

hlswsv351#

您未正确使用"选择"。
这对我很有效:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 5)

url = "https://stats.oecd.org/Index.aspx?DataSetCode=REVDEU"
driver.get(url)

select = Select(driver.find_element(By.ID, 'PAGE'))
select.select_by_value('2')

结果是

相关问题