前面章节分别介绍了**用WebDriver进行元素定位**,**用WebElement来对元素操作**。本章节对`WebDriver的一些常用方法`进行补充介绍,这一章节的内容也是蛮重要的,希望大家都掌握,否则在实际实战中,会遇到很多的困扰。 [TOC] ## WebDriver 常用方法 ```python from selenium import webdriver # 这里会打开一个Chrome浏览器空白窗口 driver = webdriver.Chrome() ``` ### 在当前浏览器session中,加载一个页面 `get(self, url)` ```python # 这里浏览器会导航到百度首页 driver.get("www.baidu.com") ``` ### 关闭当前页面 `close(self)` ```python # 这里会将当期的页面关闭掉 driver.close() ``` ### 隐式等待 `implicitly_wait(self, time_to_wait)` 设置一个固定的超时时间(s),等待一个元素被发现或名字执行完成。在一个session中,只需要设置一次,再整个session有效期内有效。当达到固定的超时时间时,如果元素还没找到,则会抛出超时异常。如果未达到超时时间,会一直重试。 ```python # 设置全局的元素等待时间30s,如果30s内找到元素,继续往下执行,如果超过30s都没有找到元素,则抛出异常。 driver.implicitly_wait(30) ``` ### 注销driver,并且关闭浏览器 `quit(self)` ```python # 这里会将整个浏览器都关闭 driver.quit() ``` ### 浏览器前进 `forward(self)` ```python # 模拟点击浏览器的“向前-->” driver.forward() ``` ### 浏览器后退 `back(self)` ```python # 模拟点击浏览器的“后退<--" driver.back() ``` ### 刷新当前页面 `refresh(self)` ```python # 模拟F5 刷新当前页面 driver.refresh() ``` ### 将当期页面截图,保存为png格式 `get_screenshot_as_file(self, filename)` filename必须要以.png为后缀 ```python # 截图保存到images/login.png driver.get_screenshot_as_file("images/login.png") ``` ### 在当前会话中添加cookie `add_cookie(self, cookie_dict)` 参数cookie_dict 是一个字典类型,至少要包含keys:“name”和“value”,可选的key还可以有“path", "domain", "secure", "expiry" ```python # Go to the correct domain driver.get("http://www.example.com") # Now set the cookie. This one's valid for the entire domain cookie = {'name':'foo', 'value':'bar'} driver.add_cookie(cookie) ``` ### 获取当前session的全部cookie `get_cookies(self)` ### 获取当前session中的指定cookie `get_cookie(self, name)` ### 删除当前session中的指定cookie `delete_cookie(self, name)` ### 删除当前session中的全部cookie `delete_all_cookies(self)` ### 在当前页面中,同步执行JavaScript语句 `execute_script(self, script, *args)` ```python #在页面中直接执行js driver.execute_script('$("#btn1").fadeOut();') # 直接调用执行js语句 js = "var q=document.getElementById('username');q.style.border='1px solid red';q.value='PTQA TEST' " driver.execute_script(js) ``` ```python # 在某个已经定位的元素上执行js button = driver.find_element_by_class_name('btn') driver.execute_script('$(arguments[0]).click()', button) ``` 滚动条滚动举例: ```python # 控制div滚动条滚动 js = '$(".modal-body").scrollTop(10000)' driver.execute_script(js) ``` ```python # 控制浏览器的滚动条 js = 'window.scrollTo(0, document.body.scrollHeight);' driver.execute_script(js) ``` ### 在当前页面中,异步执行JavaScript语句 `execute_async_script(self, script, *args)` ### 设置js超时时间 `set_script_timeout(self, time_to_wait)` ### 设置页面加载超时时间 `set_page_load_timeout(self, time_to_wait)` ### 窗口最大化 `maximize_window(self)` ### 窗口全屏 `fullscreen_window(self)` ### 窗口最小化 `minimize_window(self)` ### 设置当期窗口大小 `set_window_size(self, width, height, windowHandle='current')` ### 获取当前窗口大小 `get_window_size(self, windowHandle='current')` ## WebDriver属性方法 ### 返回当前页面的title ```python # 获得当前页面的title title = driver.title ``` ### 返回当前页面的url ```python # 获得当前页面的url url = driver.current_url ``` ### 返回当前页面的源码 ```python # 获得当前页面的源码 source = driver.page_source ``` ### 返回当前窗口的句柄 ```python # 获得当前窗口的句柄 handle = driver.current_window_handle ``` ### 返回当前浏览器的所有窗口句柄 ```python # 获得当前浏览器的所有窗口句柄 handles = driver.window_handles ``` ### 切换对象(alert,frame,window) `switch_to`: 返回一个SwitchTo类实例对象。 *切换到弹窗* `alert = driver.switch_to.alert` ```python # 切换到当前的弹窗,返回Alert类的一个实例对象 alert = driver.switch_to.alert # 获取alert中的文本 alert.text # 确定 alert.accept() # 取消、关闭 alert.dismiss() # 发送文本信息 alert.send_keys(keysToSend) ``` *切换到默认iframe* `driver.switch_to_default_content()` *切换到指定iframe* `driver.switch_to.frame('frame_name')` `driver.switch_to.frame(1)` *切换到父iframe* `driver.switch_to.parent_frame()` *切换到windows窗口* `driver.switch_to.window('windowName')` ## 给浏览器添加设置项ChromeOptions ```python options = webdriver.ChromeOptions() options.add_argument('xxxx') driver = webdriver.Chrome(chrome_options=options) ``` ### 添加浏览器 User-Agent ```python options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30') ``` ### 无界面运行 ```python options.add_argument('--headless') ``` Chrome浏览器版本为60以上才支持 <hr style="margin-top:50px"> <section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 10px;box-sizing: border-box;"><section class="" style="display: inline-block;width: 100%;border-width: 5px;border-style: double;border-color: rgb(23, 22, 24);padding: 10px;border-radius: 2px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(20px, 0px, 0px);-webkit-transform: translate3d(20px, 0px, 0px);-moz-transform: translate3d(20px, 0px, 0px);-o-transform: translate3d(20px, 0px, 0px);font-size: 11px;margin: -50px 0% 0px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;width: 7em;height: 7em;display: inline-block;vertical-align: bottom;border-radius: 100%;border-width: 4px;border-style: double;border-color: rgb(23, 22, 24);background-position: center center;background-repeat: no-repeat;background-size: cover;background-image: url(&quot;http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n&quot;);"><section class="" style="width: 100%;height: 100%;overflow: hidden;box-sizing: border-box;"><img class="" data-ratio="0.6012024" data-w="499" data-src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" style="opacity: 0; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" data-fail="0"></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 30px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 0px;box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">微信公众号:</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;">python测试开发圈</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;"><br style="box-sizing: border-box;"></p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="text-align: center;margin: 10px 0% 0px;box-sizing: border-box;"><section class="" style="max-width: 100%;vertical-align: middle;display: inline-block;border-width: 0px;border-radius: 0px;box-shadow: rgb(0, 0, 0) 0px 0px 0px;width: 90%;overflow: hidden !important;box-sizing: border-box;"><img data-ratio="1" data-w="430" data-src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" style="vertical-align: middle; max-width: 100%; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" class="" src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" data-fail="0"></section></section></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 0px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(5px, 0px, 0px);-webkit-transform: translate3d(5px, 0px, 0px);-moz-transform: translate3d(5px, 0px, 0px);-o-transform: translate3d(5px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">一起分享学习与成长路线</p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(10px, 0px, 0px);-webkit-transform: translate3d(10px, 0px, 0px);-moz-transform: translate3d(10px, 0px, 0px);-o-transform: translate3d(10px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">长按(或扫描)二维码关注</p></section></section></section></section></section></section></section></section></section>