具体视频教程可以看这个up
https://www.bilibili.com/video/BV1xr4y1m7KG/?spm_id_from=333.337.top_right_bar_window_default_collection.content.click&vd_source=53152588b43545040e102ea9639dd461
因为selenium更新所有有些地方需要修改,配置环境变量
#!/usr/bin/python3 # @Time : 2024-08-04 下午 8:25 # @Author : MingYangHe # @Email : 1780040868@qq.com # https://www.bilibili.com/video/BV1xr4y1m7KG/?spm_id_from=333.337.top_right_bar_window_default_collection.content.click&vd_source=53152588b43545040e102ea9639dd461 from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select import time chromedriver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe" # 创建一个Service对象,并传递chromedriver的路径 service = Service(executable_path=chromedriver_path) # 创建WebDriver对象,不再使用executable_path参数 driver = webdriver.Chrome(service=service) driver.implicitly_wait(10) # 隐式等待页面加载完成,最长等10秒 driver.get("https://www.bv2008.cn/app/user/register.php?type=org") print("打开网页") # 选择元素 根据id 或 css(可以右键直接复制selector) # element1 = driver.find_element(By.ID, "login_email") # element1.send_keys("1780040868@qq.com") # 也可以合二为一 driver.find_element(By.ID, "login_email").send_keys("1780040868@qq.com") # element2 = driver.find_element(By.CSS_SELECTOR, # "#reg_org > table > tbody:nth-child(1) > tr:nth-child(8) > td:nth-child(2) > a") # element2.click() # 也可以合二为一 # driver.find_element(By.CSS_SELECTOR, # "#reg_org > table > tbody:nth-child(1) > tr:nth-child(8) > td:nth-child(2) > a").click() # 根据class 或 tag_name 会找到多个元素,返回一个列表 # elements =driver.find_elements(By.CLASS_NAME, "btn") # 下拉框根据value选择 # element3 = driver.find_element(By.ID, "org_type3") # s = Select(element3) # s.select_by_value("4870") # 合三为一 # Select(driver.find_element(By.ID, "org_type3")).select_by_value("4870") # 下拉框根据visible_text选择 Select(driver.find_element(By.ID, "org_type3")).select_by_visible_text("党政机关") # 点击选择 跳转到iframe driver.find_element(By.CSS_SELECTOR, "#org_record3 > tr:nth-child(1) > td:nth-child(2) > a").click() driver.implicitly_wait(10) # 隐式等待页面加载完成,最长等10秒 # 切换iframe # driver.switch_to.frame("iframe_info") driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "body > div.div_tips > iframe")) driver.find_element(By.CSS_SELECTOR, "#org_list > ul > li > span.son > ul > li:nth-child(1) > a:nth-child(3)").click() # 切换回原页面 driver.switch_to.default_content() time.sleep(1) driver.find_element(By.CSS_SELECTOR, "body > div.nav > div > a:nth-child(2)").click() print("点击志愿项目,跳转到新的页面") driver.find_element(By.CSS_SELECTOR, 'body > div.main > div:nth-child(1) > ul > li:nth-child(2) > p > a').click() print("点击新页面中的一个元素") for window_handle in driver.window_handles: driver.switch_to.window(window_handle) # 如果当前页面有需要的信息,就跳出循环,停在当前页面 if "回家" in driver.title: break print("操作完成") time.sleep(10) driver.quit() print("等待10秒再关闭")