本文主要是介绍Selenium - 翻页(常用代码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
本文基于 macOS 10.15.7 | selenium - Version: 4.21.0 | Python 3.11.5
import time
import os
import sys
from selenium import webdriver
from selenium.webdriver.common.by import Bylist_path = 'list.txt'def next_page(driver):# 示例:获取页面列表连接数据a_nodes = driver.find_elements(by=By.XPATH, value='//div[@class="xxx"]//a')print(len(a_nodes)) for anode in a_nodes: # print(anode.get_attribute('href'))with open(list_path, 'a') as fa:fa.write(anode.get_attribute('href').strip() + '\n')next_btn = driver.find_elements(by=By.XPATH, value='//a[@class="next"]')[-1] print('-- next_btn : ', next_btn) status = next_btn.get_attribute('aria-disabled') print('-- next status : ', status) if status == 'true':return next_btn.click() time.sleep(5) next_page(driver)def all_page():options = webdriver.ChromeOptions()# options.add_argument('headless') options.add_argument('window-size=1920x1080')options.add_argument('start-maximized')options.add_argument('blink-settings=imagesEnabled=false')driver = webdriver.Chrome(options=options) driver.set_page_load_timeout(30) url = 'xxxx'driver.get(url) time.sleep(5) next_page(driver)all_page()
2024-06-17
这篇关于Selenium - 翻页(常用代码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!