selenium操作网页再练手
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | # coding:utf-8 from selenium.common import NoSuchElementException, TimeoutException, StaleElementReferenceException from selenium.webdriver.support import expected_conditions as EC from tkinter import * import threading from tkinter import ttk import pandas as pd from selenium.webdriver.support.wait import WebDriverWait import time from selenium import webdriver from selenium.webdriver.common. by import By def _OA_selenium(): driver = webdriver.Chrome() driver. get ( "http://192.168.1.9/" ) # 窗口最大化 driver.maximize_window() # 隐式等待 driver.implicitly_wait(30) # 登录 driver.find_element(By.ID, "loginid" ).send_keys( "abc" ) driver.find_element(By.ID, "userpassword" ).send_keys( "123" ) driver.find_element(By.ID, "submit" ).click() # 显式等待 wait = WebDriverWait(driver, 30, 0.5) flag = True while flag: try : el = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="tabcontant_655"]/div[1]/div/div/div/div/div/div/div/div/div/div/div/span/div/table/tbody/tr/td[2]/span/a' ))) el.click() driver.implicitly_wait(5) # 切换到新标签页 driver.switch_to.window(driver.window_handles[1]) el = wait.until(EC.presence_of_element_located( (By.XPATH, '/html/body/div[1]/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div[2]/div/div[1]/span[1]' ))) el.click() # 转入iframe fr = wait.until(EC.presence_of_element_located( (By.XPATH, '//*[@id="cke_1_contents"]/iframe' ))) driver.switch_to.frame(fr) # 括号里可以是iframe标签的id或name el = wait.until(EC.presence_of_element_located( (By.XPATH, '/html/body/p' ))) el.click() content_body = driver.find_element(By.TAG_NAME, 'body' ) content_body.send_keys( "同意" ) driver.switch_to.default_content() el = wait.until(EC.presence_of_element_located( (By.XPATH, '/html/body/div[1]/div/div/div[2]/div[1]/div[1]/div/div[3]/div/div[2]/div/span[1]/button/div' ))) el.click() el = wait.until(EC.presence_of_element_located( (By.XPATH, '/html/body/div[9]/div/div[2]/div/div[1]/div/div/div[2]/button/span' ))) el.click() driver.switch_to.window(driver.window_handles[0]) except (NoSuchElementException, TimeoutException,StaleElementReferenceException): flag =False driver.quit() #el_list = driver.find_elements(By.XPATH, '//*[@id="tabcontant_655"]/div[1]/div/div/div/div/div/div/div/div/div/div/div/span/div/table/tbody/tr/td[2]/span/a') _OA_selenium() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
2020-04-13 python +adb控制手机自动化签到