pythonGUI自动化:selenium常用的操作
- 实现代码如下:
复制代码
from selenium import webdriver
import time
#谷歌浏览器
wd=webdriver.Chrome()
#火狐浏览器
#wd=webdriver.Firefox()
#IE浏览器
#wd=webdriver.Ie()
#窗口最大化
wd.maximize_window()
#打开网站
wd.get(“https://www.baidu.com”)
time.sleep(2)
wd.find_element_by_id(“kw”).send_keys(“周杰伦”)
wd.find_element_by_xpath("//input[@id=‘su’]").click()
time.sleep(5)
#刷新页面
wd.refresh()
#截图
wd.get_screenshot_as_file(‘1.png’)
#关闭当前操作页面
wd.close()
#关闭所有页面
wd.quit()
复制代码
2. 元素动作
click() 点击
send_keys() 传入值
text 获取内容
rect 获取位置信息
clear() 清空内容
is_selected() 判断选择框是否选中(返回true或false)
- 焦点切换
1. 切换窗口
h=wd.window_handles
wd.switch_to.window(h[-1])
2. iframe内嵌页面
wd.switch_to.frame(参数) 若有两个frame,要切换两次
wd.switch_to.default_content()
3. 系统级警告窗口
ele=wd.switch_to.alert
ele.accept() 点击确认按钮
ele.dismiss() 点击取消按钮
ele.text 获取对话框的提示信息的文本内容
ele.send_keys() 输入内容
- 等待时间
1. 强制等待
time.sleep(5)
2. 隐式等待
wd. implicitly_wait(20)
3. 显式等待
WebDriverWait(wd,20,0.5).until(lambda wd: wd.find_element_by_id(“kw”)).send_keys(“yx”)
- 下拉框
下拉框的选择
Select(wd.find_element_by_id(‘scope’)).select_by_visible_text(“所有项目”)
Select(wd.find_element_by_id(‘scope’)).select_by_index(2)
Select(wd.find_element_by_id(‘scope’)).select_by_value(“0”)
增加下拉框的内容
wd.execute_script(“document.getElementById(‘scope’).innerHTML+=‘test’;”)
- 滚动条
纵向底部
js1 = ‘document.getElementById(“yx”).scrollTop=10000;’
driver.execute_script(js1)
time.sleep(5)
纵向顶部
js2 = ‘document.getElementById(“yx”).scrollTop=0;’
driver.execute_script(js2)
time.sleep(5)
横向右侧
js3=‘document.getElementById(“yx”).scrollLeft=10000;’
driver.execute_script(js3)
time.sleep(5)
横向左侧
js4=‘document.getElementById(“yx”).scrollLeft=0;’
driver.execute_script(js4)
time.sleep(5)
注:
1. 操作滚动条前后需要加休眠时间
2. 可以修改scrollTop 的值,来定位滚动条的位置,0是最上面,10000是最底部
3. 可以修改scrollLeft 的值,来定位滚动条的位置,0是最左侧,10000是最右侧
- 修改只读属性
wd.execute_script(“document.getElementById(“yx”).readOnly=false;”)
wd.find_element_by_id(‘yx’).clear()
time.sleep(5)
wd.find_element_by_id(‘yx’).send_keys(‘5646’)
- 鼠标事件
右击
webdriver.ActionChains(wd).context_click(wd.find_element_by_id(‘yx’)).perform()
双击
webdriver.ActionChains(wd).double_click(wd.find_element_by_id(‘yx’)).perform()
悬停
webdriver.ActionChains(wd).move_to_element(wd.find_element_by_id(‘yx’)).perform()
- 键盘事件
from selenium.webdriver.common.keys import Keys
wd.find_element_by_id(‘yx’).send_keys(Keys.CONTROL,‘c’) ctrl+c
wd.find_element_by_id(‘yx’).send_keys(Keys.CONTROL,‘v’) ctrl+v
- 文件上传
复制代码
方法一
wd.find_element_by_id(“fileToUpload”).send_keys(r"C:\Users\Administrator\Desktop\qq.txt")
wd.find_element_by_id(“buttonUpload”).click()
方法二
import uiautomation
wd.find_element_by_id(“fileToUpload”).click()
window=uiautomation.WindowControl(Name=‘打开’)
window.SetTopmost(True)
window.EditControl(AutomationId=‘1148’).SendKeys(r"C:\Users\Administrator\Desktop\qq.txt")
time.sleep(2)
window.Click(628,633)
方法三
from pykeyboard import PyKeyboard
from pymouse import PyMouse
p=PyKeyboard()
m=PyMouse()
m.click(497,430)
time.sleep(2)
p.type_string(r"C:\Users\Administrator\Desktop\qq.txt")
time.sleep(2)
p.press_keys([p.alt_key,‘o’])
复制代码
11. 定位表格
一般选用xpath
例:定位4的那个单元格 //*[@id=‘yx’]/tr[2]/td[1]