Python Selenium循环通过选择菜单

2024-09-29 21:49:51 发布

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

我需要循环浏览此页面上的选择菜单:

https://printcopy.info/?mod=erc&brand=Kyocera&model=TASKalfa+307ci&page=1

选择菜单有一个显示:无,因此我执行了以下操作以获取值:

    element = driver.find_element_by_id('selectNumPages')   
    driver.execute_script("return arguments[0].removeAttribute('style');", element)
    select = Select(driver.find_element_by_id('selectNumPages'))
    for opt in select.options:
        print(opt.text)

这将输出选择菜单中的每个值,现在我需要单击每个值,以便在每页上刮取数据

我试过:

button = driver.find_element_by_id("selectNumPages-button")
button.click()

单击“选择”菜单,但页面url不变

任何帮助都将不胜感激

更新

我最终使用了ActionChains


Tags: httpsinfoidmodbydriver菜单button
2条回答

这是自定义下拉列表。您可以参考以下代码:

driver.get('https://printcopy.info/?mod=erc&brand=Kyocera&model=TASKalfa+307ci&page=1')
driver.maximize_window()
wait = WebDriverWait(driver, 15)

selector = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[@id='selectNumPages-button']//span[@class='ui-selectmenu-icon ui-icon ui-icon-triangle-1-s']")))
actionChains = ActionChains(driver)
actionChains.move_to_element(selector).click().perform()
names=wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//ul[@id='selectNumPages-menu']//li")))
for name in names:
    print(name.text)

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

如果要循环浏览页面,则无需单击每个页面。您只能获取每个页面的url

https://printcopy.info/?mod=erc&brand=Kyocera&model=TASKalfa+307ci&page=1

如您所见,这里有“page=1”。您可以使用它来循环浏览页面。总共有23页

page_num = 23

for i in range(1,23):
    pg = str(i)
    driver.get('https://printcopy.info/?mod=erc&brand=Kyocera&model=TASKalfa+307ci&page='.pg)

然后只需选择您需要的项目

另外,我也是新手,如果我错了,请纠正我

相关问题 更多 >

    热门问题