pu369com

selenium操作网页再练手

 

# 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()

  

posted on 2023-04-13 17:57  pu369com  阅读(16)  评论(0编辑  收藏  举报

导航