一个selenium的自动化操作实例

代码片段:

from win32com import client as wc
from docx import Document
import os
import logging
from tkinter.filedialog import askopenfilename
from tkinter import *
import tkinter.messagebox
from tkinter import ttk,Toplevel
from selenium import webdriver
import time
import threading
import pythoncom
from webdriver_manager.chrome import ChromeDriverManager


def context_upload(self):
  pythoncom.CoInitialize()
  
    driver.find_element_by_xpath(r'//*[@id="userId"]').send_keys(self.username)
    driver.implicitly_wait(1)
    # 输入密码
    driver.find_element_by_xpath(r'//*[@id="password"]').send_keys(self.password)
    driver.implicitly_wait(1)
    # 确认登陆
    driver.find_element_by_xpath(r'//*[@id="tabLogin"]/tbody/tr/td/div/form/input[3]').click()
    driver.implicitly_wait(1)
    # 获取当前标签页的句柄
    windows = driver.current_window_handle

    
	# 点击新增按钮(需要进入iframe框架)
	iframe = self.driver.find_element_by_class_name('frameMain')
	driver.switch_to.frame(iframe)
	driver.find_element_by_xpath(
	  r'//*[@id="divToolbar"]/div[1]/table/tbody/tr/td[2]/nobr').click()
	  
	all_handles = driver.window_handles
	print(r'windows\'s handleis %s' % windows)
	for handle in all_handles:
	  print(r'current hanele---->%s' % handle)
	  if handle != windows:
		driver.switch_to.window(handle)
		
	driver.find_element_by_xpath(r'//*[@id="FLD_INFOCATEGORY"]').send_keys(r'xx')
	driver.find_element_by_xpath(r'//*[@id="SPN_ASKINFOFLAG"]/span/label[2]/input').click()
	driver.find_element_by_xpath(r'//*[@id="FLD_C_Subject"]').send_keys(title)
	driver.find_element_by_xpath(r'//*[@id="FLD_CONTENT"]').send_keys(content)
	driver.find_element_by_xpath(r'//*[@id="FLD_SOURCECONTENT"]').send_keys(url)
	driver.find_element_by_xpath(r'//*[@id="FLD_ISSUELEADER"]').send_keys(officemannger)
	driver.find_element_by_xpath(r'//*[@id="FLD_EDITOR"]').send_keys(officemannger)


    driver.find_element_by_xpath(
              r'//*[@id="divToolbar"]/div[1]/table/tbody/tr/td[2]/nobr').click()
    driver.switch_to.window(windows)

以下几个要注意的地方:

1、打开驱动有两种方式,一种是指定路径,一种通过drivermanger自己下载

try:
  # path = os.getcwd()
  # self.driverpath = path + os.sep + 'chromedriver.exe'
  # driver = webdriver.Chrome(executable_path = self.driverpath) //指定路径
  driver = webdriver.Chrome(ChromeDriverManager().install())  //自己下载

  except Exception as e:
    self.log.error(r'浏览器驱动错误%s' % e)
    driver.quit()            

2、关于退出

driver.quit()
每一次任务结束或者报错,都应该使用quit关闭。不然会导致winerror6

3、获取窗口,切换窗口

windows = driver.current_window_handle
all_handles = driver.window_handles
  print(r'windows\'s handleis %s' % windows)
  for handle in all_handles:
    print(r'current hanele---->%s' % handle)
    if handle != windows:
      driver.switch_to.window(handle)  //切换焦点

4、iframe框架的click

# 点击新增按钮(需要进入iframe框架)
  iframe = self.driver.find_element_by_class_name('frameMain')
  driver.switch_to.frame(iframe)
  driver.find_element_by_xpath(
      r'//*[@id="divToolbar"]/div[1]/table/tbody/tr/td[2]/nobr').click()

5、等待加载,解决报错找不到元素

参考文档
//隐式等待,全局的设置一次就可以了
driver.implicitly_wait(1)

//显示等待
WebDriverWait(self.driver,10).until(expected_conditions.presence_of_element_located(By.XPATH,'//*[@id="divLeftTree"]/div[1]/span'))
//强制等待
time.sleep(5)
我是用sleep解决的,其他两个方法测试没有成功

posted @ 2020-12-25 17:11  名字很长容易被惦记  阅读(272)  评论(0编辑  收藏  举报