1、刷新当前页面
.refresh()
# 刷新当前页面
driver.refresh()
2、获取本页面的URL
.current_url
用处:
一般URL可以帮助我们判断跳转的页面是否正确,或者URL中部分字段可以作为我们自动化测试脚本期待结果的一部分。
print(driver.current_url)
3、页面标题
获取当前页面标题
.title
# 获取当前页面标题显示的字段
print(driver.title)
- 断言页面标题
# 1) 包含判断
# assert:断言,声称
try:
assert "百度一下" in driver.title
print("断言测试成功.")
except Exception as e:
print("断言失败.",format(e))
# 2) 完全相等判断
if "百度一下,你就知道" == driver.title:
print("成功.")
else:
print("失败.")
print(driver.title)
4、新建标签页
用js实现如下:
try:
# 新标签页,此处用js实现,在有些博客上显示使用
# driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL, 't')
# 我这测试无效,原因不知,于是采用如下方法
js = "window.open('http://www.acfun.cn/')"
driver.execute_script(js)
# 切换到新的窗口
handles = driver.window_handles # 获取窗口句柄
driver.switch_to.window(handles[-1]) # 切换到最后一个既最新打开的窗口
# 先切换窗口再打开新网址,才是在新窗口打开网址,不然还是在原来的百度页面打开此网址
driver.get('http://map.baidu.com/')
except Exception as e:
print("发现异常,",format(e))
5、页面前进、后退
前进: .forward()
后退: .back()
driver.get("https://www.baidu.com")
time.sleep(2)
'''前进,后退'''
elem_news = driver.find_element_by_link_text("新闻").click() # 点击进入新闻
time.sleep(2)
driver.back() # 后退到百度首页
time.sleep(2)
driver.forward() # 从百度前进到新闻页
time.sleep(2)
6、获取浏览器版本号
.capabilities[‘version’]
# 获取浏览器版本号
"""
Creates a new session with the desired capabilities.
:Args:
- browser_name - The name of the browser to request.
- version - Which browser version to request.
- platform - Which platform to request the browser on.
- javascript_enabled - Whether the new session should support JavaScript.
- browser_profile - A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object. Only used if Firefox is requested.
"""
print(driver.capabilities['version'])