import time
import win32gui
import uiautomation as auto
import win32con
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
获取driver
driver = webdriver.Chrome
driver = webdriver.Firefox
time_long = 30
xpath = “//div[@id=‘123’]”
判断元素是否可见
WebDriverWait(driver, time_long).until(expected_conditions.visibility_of_element_located((By.XPATH, xpath)))
判断元素是否不可见
WebDriverWait(driver, time_long).until(expected_conditions.invisibility_of_element_located((By.XPATH, xpath)))
判断元素是否可点击
WebDriverWait(driver, time_long).until(expected_conditions.element_to_be_clickable((By.XPATH, xpath)))
判断元素是否存在
WebDriverWait(driver, time_long).until(expected_conditions.presence_of_element_located((By.XPATH, xpath)))
获取元素的值
driver.find_element_by_xpath(xpath).text
获取某一个属性
driver.find_element_by_xpath(xpath).get_attribute(“value”)
driver.find_element_by_xpath(xpath).get_attribute(“outerHTML”)
driver.find_element_by_xpath(xpath).get_attribute(“innerHTML”)
获取元素
element = driver.find_element_by_xpath(xpath)
点击某一个元素
def click():
element.click()
右击
def right_click():
ActionChains(driver).context_click(element).perform()
双击
def double_click():
ActionChains(driver).double_click(element).perform()
js方式点击(比如点击的元素被另外一个窗口遮住了,传统的点击方式就点击不到了)
def js_click():
driver.execute_script(“arguments[0].click()”, element)
移除某一个属性
def js_remove_attr(attr):
driver.execute_script(“arguments[0].removeAttribute(’%s’)” % attr, element)
设置一个属性
def js_set_attr(attr, value):
driver.execute_script(“arguments[0].setAttribute(’%s’,’%s’)” % (attr, value), element)
输入值
def input(value, readonly=False):
if readonly:
js_remove_attr(“readonly”)
element.clear()
for index in str(value):
# 输入值,这里最好循环单个输入,防止输入的内容丢失
driver.find_element_by_xpath(xpath).send_keys(index)
选择元素
def select():
# 判断元素(如checkbox)是否已被选择
if not element.is_selected():
js_click()
取消选择
def unselect():
if element.is_selected():
js_click()
等待窗口打开
def wait_for_window_open(title):
try_times = 0
while 0 == win32gui.FindWindow("#32770", title):
if 30 == try_times:
raise Exception(“window[%s] is not opened” % title)
else:
try_times += 1
time.sleep(2)
continue
return win32gui.FindWindow("#32770", title)
def wait_for_window_ex_open(hwnd, cls):
try_times = 0
while 0 == win32gui.FindWindowEx(hwnd, 0, cls, None):
if 30 == try_times:
raise Exception(“window[%s] is not opened” % cls)
else:
try_times += 1
time.sleep(1)
continue
return win32gui.FindWindowEx(hwnd, 0, cls, None)
上传文件
def file_upload(file_path):
click()
is_chrome = True
is_firefox = True
if is_chrome:
dialog = wait_for_window_open(“打开”)
elif is_firefox:
dialog = wait_for_window_open(“文件上传”)
comboboxex32 = wait_for_window_ex_open(dialog, “ComboBoxEx32”)
combobox = wait_for_window_ex_open(comboboxex32, “ComboBox”)
edit = wait_for_window_ex_open(combobox, “Edit”)
button = wait_for_window_ex_open(dialog, “Button”)
win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_path)
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
窗口操作
def window_control():
is_chrome = True
is_firefox = True
if is_chrome:
win = auto.PaneControl(searchDepth=1, ClassName=“Chrome_WidgetWin_1”)
win.SetFocus()
check = win.CheckBoxControl(searchDepth=6, Name=“窗口打开”)
if 0 == check.GetTogglePattern().ToggleState:
check.Click()
button = win.ButtonControl(searchDepth=6, Name=“开”)
button.Click()
elif is_firefox:
win = auto.WindowControl(searchDepth=1, ClassName=“MozillaDialogClass”)
win.SetFocus()
check = win.CheckBoxControl(searchDepth=6, Name=“窗口打开”)
if 0 == check.GetTogglePattern().ToggleState:
check.Click()
button = win.ButtonControl(searchDepth=6, Name=“开”)
xpath查找元素规则
1 有固定id的则按照固定id来找
xpath = “//div[@id=‘yw’]/div/span”
2 没有固定id的用其他属性来找
xpath = “//div[@role=‘alert’]”
xpath = “//div[contains(@class, ‘el-popper’) and not(contains(@style, ‘display: none’))]”
3 这两种查找方式虽然都是判断span里没有没有某一个值,但是获取的element不一样,一个是到tr层,一个是到span层
text = “陈宫”
xpath = “td[2]/tr/span[text()=’%s’]” % text
xpath = “td[2]/tr[span = ‘%s’]” % text