常见操作整理:
driver = webdriver.Chrome() findElement = find_element……
点击/清除/关闭:click/clear/close
前进/后退/刷新:driver.back/forward/refresh
浏览器窗口最大化/最小化:driver.maximizeWindow/minimizeWindow
获取/设置窗口位置:driver.get_window_position/set_window_position
获取窗口大小:driver.get_window_size
获取标题属性值:driver.title
获取HTML原代码:driver.page_source
获取当前页的url地址:driver.current_url
获取当前/所有窗口句柄:driver.current_window_handle/window_handles
切换窗口:driver.switch_to.window(now_handle)
对屏幕截图:result = self.driver.get_screenshot_as_file(r“c:\scrren.png”)
获取元素的基本信息: findElement.tagname/size/text
判断元素是否可见/可操作/可选择:findElement.is_displayed/is_enabled/is_selected
获取页面元素属性:findElement.get_attribute(“name/value”)
获取css属性:findElement.value_of_css_property()
操作frame和iframe中的元素:driver.switch_to.frame()///driver.switch_to.frame(driver.find_element_by_id("showIframe"))
操作Alert弹出框:alert = driver.switch_to.alert///点击确认:alert.accept()
操作confirm弹出框:confirm = driver.switch_to.alert///点击确认:confirm.accept() ///点击取消:confirm.dismiss()
操作prompt弹出框:prompt = driver.switch_to.alert///点击确认:accept() ///取消:dismiss()///输入:send_keys()
获取/清除cookie:driver.get_cookies/driver.delete_cookie("value")///删除所有:delete_all_cookies()
需要导入ActionChains:from selenium.webdriver import
ActionChains #用于模拟鼠标按键的包
双击某个元素:ActionChains(self.driver).double_click(findElement).perform()
拖拽页面元素:ActionChains(self.driver).drag_and_drop(“第一个元素”, “第二个元素”).perform()
右下拖动10像素:ActionChains(self.driver).drag_and_drop_by_offset(“元素”, 10,10).perform()
点击鼠标右键:ActionChains(self.driver).context_click(findElement).perform()
鼠标左键按下:ActionChains(self.driver).click_and_hold(findElement).perform()
释放一直按下的鼠标左键:ActionChains(self.driver).release(findElement).perform()
保持鼠标悬停在某元素上:ActionChains(self.driver).move_to_element(findElement).perform()
需要导入Keys: from selenium.webdriver.common.keys import Keys #提供模拟按键
send.keys(“c”, Keys.ARROW_DOWN)
Keys:ARROW_DOWN(下拉),ENTER(回车),RETURN(也相当于回车),
(按下ctrl)key_down(Keys.CONTROL).send_keys(‘a’).key_up(Keys_CONTROL)(松开)
第三方模块模拟按键:需安装pywin;导入包语句:import win32api 和import win32con
需要导入Select:from selenium.webdriver.suuport.ui importSelect
选择下拉列表元素三种方法:select_by_value()
select_by_index()
select_by_visible_text()
取消为deselect,取消全部:deselect_all,选择全部:all_selected _options
导入异常类:from selenium.common.exceptions import NoSuchElementException,TimeoutException
设置最长等待时间:self.driver.implicitly_wait(10) #隐藏等待只需设置一次,作用于整个生命周期
导入堆栈类:import tracenack
导入期望场景类:from selenium.webdriver.support import expected_conditions as EC
导入多个异常类:from selenium.common.exceptions import NoSuchElementException,TimeoutException
导入By类:from selenium.webdriver.common.by import By
导入WebDriverWait类:from selenium.webdriver.support.ui import WebDriverWait
断言:assertTure(),assertFalse(),assertEqual(),assertNotEqual()
assert u”值” in self.driver.page_source, “如果不存在,返回消息”
模拟组合键操作
import win32clipboard as w
import win32con
#读取剪贴板
def getText():
w.OpenClipboard()
d = w.GetClipboardData(win32con.CF_TEXT)
w.CloseClipboard()
return d
def setText(aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
w.CloseClipboard()