import os import psutil from selenium.webdriver.chrome.options import Options from selenium import webdriver

检查是否已经存在浏览器实例,如果存在则不再创建

for proc in psutil.process_iter(): try: if 'chrome' in proc.name() and '--remote-debugging-port=9223' in proc.cmdline(): break except: pass else: # 创建浏览器实例 os.system(r'start chrome --remote-debugging-port=9223 --user-data-dir="D:\评阅用"') options = Options() options.add_experimental_option('debuggerAddress', '127.0.0.1:9223')

在该浏览器查找标签页名称为“国家开放大学个人空间”

driver = webdriver.Chrome(options=options) driver.get('chrome://version/') profile_path = driver.execute_script('return document.querySelector('#browser-info-box tr:nth-child(2) td:nth-child(2)').textContent') profile_path = profile_path.replace('Default', 'Profile ') profile_path = profile_path.replace('\', '\') driver.quit()

在网页中查找class属性为

Android智能手机编程#

并点击

options = Options() options.add_argument(f'user-data-dir={profile_path}') options.add_experimental_option('debuggerAddress', '127.0.0.1:9223') driver = webdriver.Chrome(options=options)

driver.get('https://www.ouchn.edu.cn/') driver.maximize_window()

点击“国家开放大学个人空间”标签页

handles = driver.window_handles for handle in handles: driver.switch_to.window(handle) if '国家开放大学个人空间' in driver.title: break

在标签页中查找class属性为

Android智能手机编程#

并点击

elements = driver.find_elements_by_class_name('learning_course') for element in elements: if 'Android智能手机编程#' in element.text: element.click() break

driver.quit()

Python Selenium 自动化点击国家开放大学个人空间课程

原文地址: http://www.cveoy.top/t/topic/f2MV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录