# coding:utf-8 from selenium.common import NoSuchElementException, TimeoutException 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("123") driver.find_element(By.ID, "userpassword").send_keys("123") driver.find_element(By.ID, "submit").click() # 显式等待 wait = WebDriverWait(driver, 30, 0.5) def run(): # nonlocal driver 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]) flag = True while flag: driver.refresh() 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'))) except (NoSuchElementException, TimeoutException): driver.quit() flag = False el.click() run() # input('t') _OA_selenium()
代码中循环刷新网页不太好,应该用 find_elements 取元素后循环处理。
EC.presence_of_element_located的参数注意要用()括起来标签:练手,el,网页,selenium,driver,element,import,div,wait From: https://www.cnblogs.com/pu369/p/17315907.html