python selenium (二)
定位 单个
find_element_by_id()
find_element_by_name()
find_element_by_xpath()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_tag_name()
find_element_by_class_name()
find_element_by_css_selector()
多个元素
find_elements_by_name()
find_elements_by_xpath()
find_elements_by_link_text()
find_element_by_partial_link_text()
find_elements_by_tag_name()
find_elements_by_class_name()
find_elements_by_css_selector()
获取标签内容
src=driver.find_element_by_id('xxxx').get_attribute('src')
text_a=driver.find_element_by_id('xxxx').text
获取标签属性
link=dr.find_element_by_id('xxxx')
link.value_of_css_property('color')
link.find_element_by_tag_name('p').value_of_css_property('font')
获取标签状态 is_enable():用于存储input、select等元素的可编辑状态,可以编辑返回true,否则返回false is_displayed():判断某个元素是否存在页面上,是html代码的存在。某些情况元素的visibility为hidden或者display属性为none. isSelected():判断某个元素是否被选中。一般判断表单元素。如checkbox。
print(driver.find_element_by_id("xxx").is_displayed())
print(driver.find_element_by_id('xxxx').find_element_by_name('xxx').is_enabled())
driver.find_element_by_id('xxxx').is_selected()
s = driver.find_elements_by_css_selector("input[class='ui-checkbox ui-checkbox-system']")[0].is_selected()
print(s)
if s == False:
driver.find_elements_by_css_selector("input[class='ui-checkbox ui-checkbox-system']")[0].click()
send_keys("aaaa")
clear()
click()
move_to_element(above)
double_click()
submit()
size
back()
forward()
refresh()
close()
quit()
driver.title
driver.current_window_handle
driver.switch_to.window(driver.window_handles[-1])
move_to_element(element).perform()
context_click()
drag_and_drop(source_element, target_element)
move_to_element_with_offset(to_element, xoffset, yoffset)
driver.execute_script("arguments[0].scrollIntoView();", draggable)
ActionChains(driver).click_and_hold(draggable).perform()
ActionChains(driver).move_by_offset(xoffset=247, yoffset=0).perform()
ActionChains(driver).release().perform()
get_window_size()
set_window_size(480,800)
maximize_window()
current_url
page_source
以下部分来源:python selenium(常用关键字)
窗口滚动操作 js = “window.scrollTo(300,200)” #300表示左边距,200表示上边距 js = “document.documentElement.scrollTop=200” #向下滑动200px js = “document.documentElement.scrollLeft=300” #向右滑动300px execute_script(js)
driver.execute_script("arguments[0].scrollIntoView();", p)
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
driver.execute_script('window.scrollTo(0,0)')
线程等待(强制等待):time.sleep(s) 隐式等待:driver.implicitly_wait(s) 等待页面所以的元素加载完成,则会自动进行下一步操做,如果等待时间超过设置的时间则会报错 显式等待:等待页面特定的某个元素加载后,才会进行下一步操作,如果在设定的时间内没有找到则会报错
element = WebDriverWait(driver=dr,timeout=10,poll_frequency=0.5).until(EC.presence_of_element_located((By.ID,"kw")))
implicitly_wait()
set_page_load_timeout()
set_script_timeout()
timeout
poll_frequency
ignored_exceptions
在ifram框架内要切进来才可以操作,然后再切出去
dr.switch_to_frame("iframeid")
dr.switch_to.default_content()
dr.switch_to.parent_frame()
弹窗 switch_to.alert.accept() #确定 switch_to.alert.dismiss() #取消 switch_to.alert.text #获取弹框信息
WebDriverWait() 一般由 until() 或 until_not() 方法配合使用
EC 预置条件模块具有很多类型的判断:
presence_of_element_located
presence_of_all_elements_located
text_to_be_present_in_element_value
presence_of_all_elements_located
from selenium.webdriver.common.keys import Keys
Keys.BACK_SPACE 删除输入框内结尾的单个字符 Keys.SPACE 空格 Keys.ENTER 点击enter键 Keys.CONTROL,“a” 全选 Keys.CONTROL,“x” 剪切 Keys.CONTROL,“c” 复制 Keys.CONTROL,“v” 粘贴
dr.find_element_by_id("kw").send_keys("xxx", Keys.SPACE, "xx内容xxx", Keys.ENTER)
time.sleep(2)
dr.find_element_by_id("kw").send_keys(Keys.BACK_SPACE * 3)
time.sleep(2)
dr.find_element_by_id("kw").send_keys(Keys.CONTROL,"a")
dr.find_element_by_id("kw").send_keys(Keys.LEFT * 3)
time.sleep(2)
dr.find_element_by_id("kw").send_keys(Keys.SHIFT, Keys.RIGHT * 3)
time.sleep(2)
断言 assert断言(基于unittest)
assertIn(arg1, arg2, msg=None)
assertNotIn(arg1, arg2, msg=None)
assertEqual(arg1, arg2, msg=None)
assertNotEqual(arg1, arg2, msg=None)
if '保存成功' in ts.text:
print('Assertion test pass.')
else:
print('Assertion test fail.')
ts = driver.find_element_by_class_name("ivu-modal-content")
print(ts.text)
self.assertIn("保存成功", ts.text,msg='保存失败')
try:
driver.find_element_by_id("kw")
print("test pass: ID found")
except Exception as e:
print("Exception found", format(e))
https://blog.csdn.net/BEEKSTAR/article/details/78873749?utm_source=blogxgwz3
WebDriver提供许多用来与浏览器交互的功能和设置,通过WebDriver的功能和一些方法,来实现与浏览器窗口、警告、框架和弹出窗口的交互,它也提供了自动化操作浏览器导航栏、设置cookies、截屏等方便我们测试的特性。 通过WebElement可以实现与网站页面元素的交互,包括文本框、文本域、按钮、单选框、多选框、表格、行、列和div等。
|