常用方法函数
- 加载浏览器驱动: webdriver.Firefox()
- 打开页面:get()
- 关闭浏览器:quit()
- 最大化窗口: maximize_window()
- 设置窗口参数:set_window_size(600,800)
- 后退到前一页: back()
- 前进到后一页: forward()
- 刷新页面: refresh()
- 元素定位:
- id定位:find_element_by_id()
- name定位:find_element_by_name()
- class定位:find_element_by_class()
- tag定位:find_element_by_tag_name()
- link定位:find_element_by_link_text()
- partial link 定位: find_element_by_partial_link_text()
- CSS定位:find_element_by_css_selector()
- Xpath定位:
- 绝对路径:find_element_by_xpath("/html/body/div[x]/div[x]/div/div/dl[x]/dt/a")
- 元素属性:find_element_by_xpath("//unput[@id=‘kw’]")
- 层级与属性结合:find_element_by_xpath("//form[@id=‘loginForm’]/ul/input[1]")
- 逻辑运算符:find_element_by_xpath("//input[@id=‘kw’ [email protected]=‘s_ipt’]")
- 清除文本:clear()
- 模拟按键输入:send_keys(*value)11.模拟按键输入:send_keys(*value)
- 单击元素:click()
- 提交表单(相当于"回车"):submit()
- 鼠标事件:
#coding:utf-8 from selenium.webdriver.common.action_chains import ActionChains ActionChains(driver).***opration(opra)*** .perform() elemengt = driver.find_element_by_xpath("xpath") ActionChains(driver). double_click(DoubleClick) .perform()#双击 ActionChains(driver). context_click(RightClick) .perform()#右击 ActionChains(driver). drag_and_drop(Start, End) .perform()#拖放 ActionChains(driver). move_to_element(Above) .perform()#悬停 ActionChains(driver). click_and_hold(leftclick) .perform()#按下
键盘相关:
- 键盘事件:
send_keys(Keys.BACK_SPACE) = BackSpace
send_keys(Keys.SPACE) = Space
send_keys(Keys.TAB) = Tab
send_keys(Keys.ESCAPE) = Esc
send_keys(Keys.ENTER) = Enter
send_keys(Keys.CONTROL,‘a’) = Ctrl+A
send_keys(Keys.F1) = 键盘F1 - 元素等待:
- 显示等待
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC base_url = "http://www.baidu.com" driver = webdriver.Firefox() driver.implicitly_wait(5) ‘‘‘隐式等待和显示等待都存在时,超时时间取二者中较大的‘‘‘ locator = (By.ID,‘kw‘) driver.get(base_url) WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道")) ‘‘‘判断title,返回布尔值‘‘‘ WebDriverWait(driver,10).until(EC.title_contains(u"百度一下")) ‘‘‘判断title,返回布尔值‘‘‘ WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,‘kw‘))) ‘‘‘判断某个元素是否被加到了dom树里,并不代表该元素一定可见,如果定位到就返回WebElement‘‘‘ WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,‘su‘))) ‘‘‘判断某个元素是否被添加到了dom里并且可见,可见代表元素可显示且宽和高都大于0‘‘‘ WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value=‘kw‘))) ‘‘‘判断元素是否可见,如果可见就返回这个元素‘‘‘ WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,‘.mnav‘))) ‘‘‘判断是否至少有1个元素存在于dom树中,如果定位到就返回列表‘‘‘ WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,‘.mnav‘))) ‘‘‘判断是否至少有一个元素在页面中可见,如果定位到就返回列表‘‘‘ WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id=‘u1‘]/a[8]"),u‘设置‘)) ‘‘‘判断指定的元素中是否包含了预期的字符串,返回布尔值‘‘‘ WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,‘#su‘),u‘百度一下‘)) ‘‘‘判断指定元素的属性值中是否包含了预期的字符串,返回布尔值‘‘‘ #WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator)) ‘‘‘判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False‘‘‘ #注意这里并没有一个frame可以切换进去 WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,‘#swfEveryCookieWrap‘))) ‘‘‘判断某个元素在是否存在于dom或不可见,如果可见返回False,不可见返回这个元素‘‘‘ #注意#swfEveryCookieWrap在此页面中是一个隐藏的元素 WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id=‘u1‘]/a[8]"))).click() ‘‘‘判断某个元素中是否可见并且是enable的,代表可点击‘‘‘ driver.find_element_by_xpath("//*[@id=‘wrapper‘]/div[6]/a[1]").click() #WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id=‘wrapper‘]/div[6]/a[1]"))).click() #WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,‘su‘))) ‘‘‘等待某个元素从dom树中移除‘‘‘ #这里没有找到合适的例子 WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id=‘nr‘]/option[1]"))) ‘‘‘判断某个元素是否被选中了,一般用在下拉列表‘‘‘ WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH,"//*[@id=‘nr‘]/option[1]"),True)) ‘‘‘判断某个元素的选中状态是否符合预期‘‘‘ WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//*[@id=‘nr‘]/option[1]"),True)) ‘‘‘判断某个元素的选中状态是否符合预期‘‘‘ driver.find_element_by_xpath(".//*[@id=‘gxszButton‘]/a[1]").click() instance = WebDriverWait(driver,10).until(EC.alert_is_present()) ‘‘‘判断页面上是否存在alert,如果有就切换到alert并返回alert的内容‘‘‘ print(instance.text) instance.accept() driver.close()
2. 隐式等待
from selenium.common.exceptions import NoSuchElementException drive.implicitly_wait(10)
表单窗口相关操作
- 多表单切换:switch_to.frame()
- 多窗口切换:switch_to.window()
当前句柄:current_window_handle
所有句柄:window_handles - 警告框处理:switch_to_alert()
text:返回所有alert/confirm/prompt中的文字信息
accept():接受现有警告框
dismiss():解散现有警告框
send_keys(keysToSend):发送文本至警告框 - cookie处理:
get_cookies():获得所有cookie信息
get_cookie(name):返回字典的key为“name”的cookie信息
add_cookie(cookie_dict):添加cookie。“cookie_dict”指字典对象,必须有name和value值
delete_cookie(name,optionsString):删除cookie信息。“name”是要删除的cookie的名称,“optionsString”是该cookie的选项,目前支持的选项包括“路径”,“域”
delete_all_cookies():删除所有cookie信息 - 窗口截图:get_screenshot_as_file()
- 关闭窗口:close()
- 生成随机数:radint()
- 获得title并打印
#coding:utf-8 from selenium import webdriver title = driver.title print(title) if title == u"百度一下,你就知道":#比较title print("title yes!") else: print("title no!") url = driver.current_url#获得当前URL并打印 print(url)
9.滚动条设置(2种方式):
# 使用scrollTop滑动到底部 js = "var action=document.documentElement.scrollTop=10000" driver.execute_script(js) # 使用scrollTo设置位置 driver.set_window_size(600, 600) js = "window.scrollTo(100,450);" driver.execute_script(js)
常用方法函数
- 加载浏览器驱动: webdriver.Firefox()
- 打开页面:get()
- 关闭浏览器:quit()
- 最大化窗口: maximize_window()
- 设置窗口参数:set_window_size(600,800)
- 后退到前一页: back()
- 前进到后一页: forward()
- 刷新页面: refresh()
- 元素定位:
- id定位:find_element_by_id()
- name定位:find_element_by_name()
- class定位:find_element_by_class()
- tag定位:find_element_by_tag_name()
- link定位:find_element_by_link_text()
- partial link 定位: find_element_by_partial_link_text()
- CSS定位:find_element_by_css_selector()
- Xpath定位:
- 绝对路径:find_element_by_xpath("/html/body/div[x]/div[x]/div/div/dl[x]/dt/a")
- 元素属性:find_element_by_xpath("//unput[@id=‘kw’]")
- 层级与属性结合:find_element_by_xpath("//form[@id=‘loginForm’]/ul/input[1]")
- 逻辑运算符:find_element_by_xpath("//input[@id=‘kw’ [email protected]=‘s_ipt’]")
- 清除文本:clear()
- 模拟按键输入:send_keys(*value)11.模拟按键输入:send_keys(*value)
- 单击元素:click()
- 提交表单(相当于"回车"):submit()
- 鼠标事件:
#coding:utf-8 from selenium.webdriver.common.action_chains import ActionChains ActionChains(driver).***opration(opra)*** .perform() elemengt = driver.find_element_by_xpath("xpath") ActionChains(driver). double_click(DoubleClick) .perform()#双击 ActionChains(driver). context_click(RightClick) .perform()#右击 ActionChains(driver). drag_and_drop(Start, End) .perform()#拖放 ActionChains(driver). move_to_element(Above) .perform()#悬停 ActionChains(driver). click_and_hold(leftclick) .perform()#按下
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 键盘事件:
send_keys(Keys.BACK_SPACE) = BackSpace
send_keys(Keys.SPACE) = Space
send_keys(Keys.TAB) = Tab
send_keys(Keys.ESCAPE) = Esc
send_keys(Keys.ENTER) = Enter
send_keys(Keys.CONTROL,‘a’) = Ctrl+A
send_keys(Keys.F1) = 键盘F1 - 元素等待:
- 显示等待
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC base_url = "http://www.baidu.com" driver = webdriver.Firefox() driver.implicitly_wait(5) ‘‘‘隐式等待和显示等待都存在时,超时时间取二者中较大的‘‘‘ locator = (By.ID,‘kw‘) driver.get(base_url) WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道")) ‘‘‘判断title,返回布尔值‘‘‘ WebDriverWait(driver,10).until(EC.title_contains(u"百度一下")) ‘‘‘判断title,返回布尔值‘‘‘ WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,‘kw‘))) ‘‘‘判断某个元素是否被加到了dom树里,并不代表该元素一定可见,如果定位到就返回WebElement‘‘‘ WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,‘su‘))) ‘‘‘判断某个元素是否被添加到了dom里并且可见,可见代表元素可显示且宽和高都大于0‘‘‘ WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value=‘kw‘))) ‘‘‘判断元素是否可见,如果可见就返回这个元素‘‘‘ WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,‘.mnav‘))) ‘‘‘判断是否至少有1个元素存在于dom树中,如果定位到就返回列表‘‘‘ WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,‘.mnav‘))) ‘‘‘判断是否至少有一个元素在页面中可见,如果定位到就返回列表‘‘‘ WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id=‘u1‘]/a[8]"),u‘设置‘)) ‘‘‘判断指定的元素中是否包含了预期的字符串,返回布尔值‘‘‘ WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,‘#su‘),u‘百度一下‘)) ‘‘‘判断指定元素的属性值中是否包含了预期的字符串,返回布尔值‘‘‘ #WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator)) ‘‘‘判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False‘‘‘ #注意这里并没有一个frame可以切换进去 WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,‘#swfEveryCookieWrap‘))) ‘‘‘判断某个元素在是否存在于dom或不可见,如果可见返回False,不可见返回这个元素‘‘‘ #注意#swfEveryCookieWrap在此页面中是一个隐藏的元素 WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id=‘u1‘]/a[8]"))).click() ‘‘‘判断某个元素中是否可见并且是enable的,代表可点击‘‘‘ driver.find_element_by_xpath("//*[@id=‘wrapper‘]/div[6]/a[1]").click() #WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id=‘wrapper‘]/div[6]/a[1]"))).click() #WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,‘su‘))) ‘‘‘等待某个元素从dom树中移除‘‘‘ #这里没有找到合适的例子 WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id=‘nr‘]/option[1]"))) ‘‘‘判断某个元素是否被选中了,一般用在下拉列表‘‘‘ WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH,"//*[@id=‘nr‘]/option[1]"),True)) ‘‘‘判断某个元素的选中状态是否符合预期‘‘‘ WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//*[@id=‘nr‘]/option[1]"),True)) ‘‘‘判断某个元素的选中状态是否符合预期‘‘‘ driver.find_element_by_xpath(".//*[@id=‘gxszButton‘]/a[1]").click() instance = WebDriverWait(driver,10).until(EC.alert_is_present()) ‘‘‘判断页面上是否存在alert,如果有就切换到alert并返回alert的内容‘‘‘ print(instance.text) instance.accept() driver.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 隐式等待
from selenium.common.exceptions import NoSuchElementException drive.implicitly_wait(10)
- 1
- 2
- 显示等待
- 多表单切换:switch_to.frame()
- 多窗口切换:switch_to.window()
当前句柄:current_window_handle
所有句柄:window_handles - 警告框处理:switch_to_alert()
text:返回所有alert/confirm/prompt中的文字信息
accept():接受现有警告框
dismiss():解散现有警告框
send_keys(keysToSend):发送文本至警告框 - cookie处理:
get_cookies():获得所有cookie信息
get_cookie(name):返回字典的key为“name”的cookie信息
add_cookie(cookie_dict):添加cookie。“cookie_dict”指字典对象,必须有name和value值
delete_cookie(name,optionsString):删除cookie信息。“name”是要删除的cookie的名称,“optionsString”是该cookie的选项,目前支持的选项包括“路径”,“域”
delete_all_cookies():删除所有cookie信息 - 窗口截图:get_screenshot_as_file()
- 关闭窗口:close()
- 生成随机数:radint()
- 获得title并打印
#coding:utf-8 from selenium import webdriver title = driver.title print(title) if title == u"百度一下,你就知道":#比较title print("title yes!") else: print("title no!") url = driver.current_url#获得当前URL并打印 print(url)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 滚动条设置(2种方式):
# 使用scrollTop滑动到底部 js = "var action=document.documentElement.scrollTop=10000" driver.execute_script(js) # 使用scrollTo设置位置 driver.set_window_size(600, 600) js = "window.scrollTo(100,450);" driver.execute_script(js)
原文地址:https://www.cnblogs.com/111testing/p/10868841.html
时间: 2024-10-26 01:41:42