前言 selenium常用方法总结
- 打开浏览器函数,"firefox"、"chrome"、"ie"、"phantomjs"
def choosebrowser(self, browser): """ 打开浏览器函数,"firefox"、"chrome"、"ie"、"phantomjs" """ try: if browser == "firefox" or browser == "Firefox" or browser == "ff": print("start browser browser :Firefox") driver = webdriver.Firefox() return driver elif browser == "chrome" or browser == "Chrome": print("start browser browser :Chrome") driver = webdriver.Chrome() return driver elif browser == "ie" or browser == "Ie": print("start browser browser :Ie") driver = webdriver.Ie('../resources/IEDriverServer.exe') return driver elif browser == "phantomjs" or browser == "Phantomjs": print("start browser browser :phantomjs") driver = webdriver.PhantomJS() return driver else: print("Not found this browser,You can use 'firefox', 'chrome', 'ie' or 'phantomjs'") except Exception as msg: print("启动浏览器出现异常:%s" % str(msg))
- 打开新的标签页
def openlable(self, driver, url):
'''
打开新的标签页
:param driver:
:param url:
:return:
'''
js = 'window.open("%s")' % url
driver.execute_script(js)
- 切换标签页
def clooselable(self, driver, number):
'''
切换标签页
:param driver:
:param number:
:return:
'''
handles = driver.window_handles
driver.switch_to.window(handles[number])
- 得到滚动高度
def getscrollheight(self, driver, number):
'''
得到滚动高度
:param driver:
:param number:
:return:
'''
js = "var q=document.documentElement.scrollTop=" str(number)
driver.execute_script(js)
- 设置滚动高度
def setscrollheight(self, driver):
'''
设置滚动高度
:param driver:
:param number:
:return:
'''
js = "document.documentElement.scrollTop"
driver.execute_script(js)
- scrollby滚动
def setscrollby(self, driver, x, y):
'''
x:必传,正数则向右滑动的像素值,负数则向左滑动的像素值
y:必传,正数则向下滑动的像素值,负数则向上滑动的像素值
:param driver:
:param x:
:param y:
:return:
'''
js = "window.scrollBy(%s,%s)" % (x, y)
driver.execute_script(js)
- scrollto滚动
def setscrollto(self, driver, x, y):
'''
x:必传,正数则向右滑动的像素值,负数则向左滑动的像素值
y:必传,正数则向下滑动的像素值,负数则向上滑动的像素值
:param driver:
:param x:
:param y:
:return:
'''
js = "window.scrollTo(%s,%s)" % (x, y)
driver.execute_script(js)
- 鼠标悬停事件
def mouseoverhang(self, driver, mouse):
'''
鼠标悬停事件
:param driver:
:param mouse:
:return:
'''
ActionChains(driver).move_to_element(mouse).perform()
- input上传文件
def fileupload(self, pic, filepath):
'''
input上传文件
:param driver:
:param pic:
:return:
'''
pic.send_keys(filepath)
- 用js方法输入日期
def datehandle(self, driver, id,date):
'''
用js方法输入日期
:param driver:
:param id:
:return:
'''
js = 'document.getElementById(%s).value="%s"' % (id,date)
driver.execute_script(js)
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。