目录
截图
- 截某个元素的图
ele = driver.find_element(By.XPATH, "//div[@class='alert alert-success alert-dismissible']").screenshot("1.png")
滚动条相关操作
- 跳转到某个元素的位子,返回改位置的X,Y
ele = driver.find_element(By.XPATH, "//input[@id='field2']").location_once_scrolled_into_view print(ele)
判断状态
-
判断是否被选中
driver.find_element(By.LINK_TEXT,"在新窗口打开百度").is_selected()
-
判断元素是否启用
driver.find_element(By.LINK_TEXT,"在新窗口打开百度").is_enabled()
-
判断元素是否可见
driver.find_element(By.LINK_TEXT,"在新窗口打开百度").is_displayed()
获取网页相关数据
- 获取网页标题
print("当前网页标题", driver.title)
- 获取当前URL
print("获取当前URL", driver.current_url)
- 获取当前网页源码
print("获取当前网页源码", driver.page_source)
- 获取浏览器名称
print("获取浏览器名称", driver.name)
浏览器操作
-
浏览器最大化
driver.maximize_window()
-
后退
driver.back()
-
前进
driver.forward()
-
关闭浏览器
driver.quit()
-
关闭当前网页
driver.close()
-
刷新当前页面
driver.refresh()
元素操作
-
获取元素标签
ele = driver.find_element(By.CSS_SELECTOR,".form-inline #account") print(ele.tag_name) # 打印出该定位的原属是一个什么标签
-
获取属性值
ele = driver.find_element(By.CSS_SELECTOR,".form-inline #account") print(ele.get_attribute("placeholder")) # 传入属性名称获取属性值
-
输入内容
ele = driver.find_element(By.CSS_SELECTOR,".form-inline #account") print(ele.sned_keys("placeholder"))
-
点击元素
ele = driver.find_element(By.CSS_SELECTOR,".form-inline #account") print(ele.click("placeholder"))
-
获取文本
ele = driver.find_element(By.CSS_SELECTOR,".form-inline #account") print(ele.text)
-
获取文本
ele = driver.find_element(By.CSS_SELECTOR,".form-inline #account").clear()
-
获取CSS属性值
ele = driver.find_element(By.XPATH, "//div[@class='alert alert-success alert-dismissible']") print(ele.value_of_css_property("position"))