一、操作目的
1.1 自動化測驗常用操作
1、定位網頁上的元素,并存盤到一個變數中;
2、對變數進行操作,比如點擊或輸入文字;
3、設定頁面元素的操作值;
二、定位方法匯總
2.1 findElement(s)
click(self, on_element=None)
click_and_hold(self, on_element=None)
context_click(self, on_element=None)
double_click(self, on_element=None)
drag_and_drop(self, source, target)
drag_and_drop_by_offset(self, source, xoffset, yoffset)
key_down(self, value, element=None)
key_up(self, value, element=None)
move_by_offset(self, xoffset, yoffset)
move_to_element(self, to_element)
move_to_element_with_offset(self, to_element, xoffset, yoffset)
pause(self, seconds)
perform(self)
release(self, on_element=None)
reset_actions(self)
send_keys(self, *keys_to_send)
send_keys_to_element(self, element, *keys_to_send)
2.2、selenium.webdriver.common.by
CLASS_NAME = 'class name'
CSS_SELECTOR = 'css selector'
ID = 'id'
LINK_TEXT = 'link text'
NAME = 'name'
PARTIAL_LINK_TEXT = 'partial link text'
TAG_NAME = 'tag name'
XPATH = 'xpath'
2.3、selenium.webdriver.common.keys
ADD = '\ue025'
ALT = '\ue00a'
ARROW_DOWN = '\ue015'
ARROW_LEFT = '\ue012'
ARROW_RIGHT = '\ue014'
ARROW_UP = '\ue013'
BACKSPACE = '\ue003'
BACK_SPACE = '\ue003'
CANCEL = '\ue001'
CLEAR = '\ue005'
COMMAND = '\ue03d'
CONTROL = '\ue009'
DECIMAL = '\ue028'
DELETE = '\ue017'
DIVIDE = '\ue029'
DOWN = '\ue015'
END = '\ue010'
ENTER = '\ue007'
EQUALS = '\ue019'
ESCAPE = '\ue00c'
F1 = '\ue031'
F10 = '\ue03a'
F11 = '\ue03b'
F12 = '\ue03c'
F2 = '\ue032'
F3 = '\ue033'
F4 = '\ue034'
F5 = '\ue035'
F6 = '\ue036'
F7 = '\ue037'
F8 = '\ue038'
F9 = '\ue039'
HELP = '\ue002'
HOME = '\ue011'
INSERT = '\ue016'
LEFT = '\ue012'
LEFT_ALT = '\ue00a'
LEFT_CONTROL = '\ue009'
LEFT_SHIFT = '\ue008'
META = '\ue03d'
MULTIPLY = '\ue024'
NULL = '\ue000'
NUMPAD0 = '\ue01a'
NUMPAD1 = '\ue01b'
NUMPAD2 = '\ue01c'
NUMPAD3 = '\ue01d'
NUMPAD4 = '\ue01e'
NUMPAD5 = '\ue01f'
NUMPAD6 = '\ue020'
NUMPAD7 = '\ue021'
NUMPAD8 = '\ue022'
NUMPAD9 = '\ue023'
PAGE_DOWN = '\ue00f'
PAGE_UP = '\ue00e'
PAUSE = '\ue00b'
RETURN = '\ue006'
RIGHT = '\ue014'
SEMICOLON = '\ue018'
SEPARATOR = '\ue026'
SHIFT = '\ue008'
SPACE = '\ue00d'
SUBTRACT = '\ue027'
TAB = '\ue004'
UP = '\ue013'
2.4、selenium.webdriver.Chrome
| 函式 | 描述 |
|---|---|
back() |
回傳 |
close() |
關閉當前視窗 |
delete_all_cookies() |
洗掉所有 cookies |
delete_cookie('name') |
按名字洗掉 cookie |
find_element(by='id', value=https://www.cnblogs.com/amnotgcs/p/None) |
查找給定識別符號的元素 |
find_element_by_class_name('name') |
通過類名來定位元素 |
find_element_by_css_selector('css_selector') |
通過 css 選擇器來定位元素 |
find_element_by_id('id') |
通過 id 來定位元素 |
find_element_by_link_text('link_text') |
通過鏈接內容來定位元素 |
find_element_by_partial_link_text('link_text') |
通過鏈接部分內容來定位元素 |
find_element_by_name('name') |
通過元素名稱來定位元素 |
find_element_tag_name('name') |
通過標簽名來定位元素 |
find_element_by_xpath('xpath') |
通過 xpath 來定位元素 |
find_elements(by='id', value=https://www.cnblogs.com/amnotgcs/p/None) |
|
find_elements_by_class_name('name') |
|
find_elements_by_css_selector('css_selector') |
|
find_elements_by_id('id') |
|
find_elements_by_link_text('text') |
|
find_elements_by_partial_link_text('link_text') |
|
find_elements_by_name('name') |
|
find_elements_by_tag_name('name') |
|
find_elements_by_xpath('xpath') |
|
forward() |
向前 |
fullscreen_window() |
全屏 |
get('url') |
訪問 url 頁面 |
get_cookie(''name') |
通過名稱獲取 cookie |
get_cookies() |
獲取所有 cookies |
get_log('log_type') |
獲取日志,型別有:browser、driver、client、server |
get_screenshot_as_base64(self) |
獲取圖片存為 base64 編碼 |
get_screenshot_as_file('file_name') |
獲取圖片存為檔案,需要指定檔案名,png 后綴 |
get_screenshot_as_png() |
將圖片存為二進制資料 |
get_window_position(windowHandle='current') |
獲取當前視窗的 x,y 位置 |
get_window_rect() |
獲取 x,y 坐標和當前視窗的寬高 |
get_window_size(windowHandle='current') |
獲取當前視窗的寬高 |
implicitly_wait('time_to_wait) |
隱式等待一個元素加載或命令執行完成的時間, |
maximize_window() |
最大化 |
minimize_window() |
最小化 |
refresh() |
重繪 |
save_screenshot('findname') |
保存螢屏截圖 |
set_page_load_timeout('time_to_wait') |
|
set_script_timeout('time_to_wait') |
|
set_window_position(x, y, windowHandle='current') |
|
set_window_rect(x, y, width, height) |
|
set_window_size(width, height, windowHandle='current') |
|
start_client() |
|
start_session(capabilities, browser_profile=None) |
|
stop_client() |
|
switch_to_active_element() |
|
switch_to_alert() |
|
switch_to_default_content() |
|
switch_to_frame('frame_reference') |
切換到指定框架 |
switch_to_window('window_name') |
切換到指定視窗 |
那么總的來說,查找元素分為以下兩類:
1、查到單個元素
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
2、查找多個元素(回傳串列)
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
上面的是公有方法,還有兩個私有方法是:
find_element()
find_elements()
# 這兩個方法使用的時候,需要提供兩個引數
# 一個是 By.xx ,這里的 xx 分別是上面列出的幾種定位方法的大寫
# 比如 find_element(By.ID, 'su')
# 比如 find_element(By.CSS_SELECTOR, 'h1')
三、撰寫實際代碼測驗
3.1 元素定位
// id 定位
WebElement username = driver.findElement(By.id("username"));
// name 定位
WebElement username = driver.findElement(By.name("username"));
// 部分鏈接文字定位
WebElement link = driver.findElement(By.partialLinkText("sog"));
// 使用標簽名定位
WebElement link = driver.findElement(By.tagName("a"));
// 使用 class 名定位
WebElement input = driver.findElement(By.className("tight"));
// 使用xpath定位
WebElement button = driver.findElement(By.xpath("/html/body/div/input[@value='https://www.cnblogs.com/amnotgcs/p/查詢']"));
// 上述xpath使用的是絕對路徑定位,當頁面發生變化時,很容易造成代碼失效,
// 所以推薦使用相對路徑,比如將上述定位改成:"//input[@value='https://www.cnblogs.com/amnotgcs/p/查詢']"
// 如果定位元素是串列時,下標從1開始
// css定位
WebElement button = driver.findElement(By.cssSelector("input#div > input#ok"));
WebElement button = driver.findElement(By.cssSelector("input#div > input + a + img"));
WebElement button = driver.findElement(By.cssSelector("input:nth-child(2)"));
WebElement button = driver.findElement(By.cssSelector("input#div :nth-child(2)"));
WebElement button = driver.findElement(By.cssSelector("a['href' ^= 'http://www.baidu.com']"));
// ^= 匹配開頭,$= 匹配結尾 *= 鏈接含有即匹配中
xpath模糊定位
| xpath函式 | 定位運算式 | 解釋 |
|---|---|---|
| Starts-with() | //img[starts-with(@alt,'div1')] | 查找圖片alt屬性開始位置包含“div1“關鍵字的頁面元素 |
| Contains() | //img[contains(@alt,'g1')] | 查找圖片alt屬性包含“gl“關鍵字的頁面元素 |
XPath 軸(Axis)定位:
| 關鍵字 | 含義 | 實體 | 解釋 |
|---|---|---|---|
| parent | 選擇當前節點的上層節點 | //img[@alt='ok']/parent::div | 查找alt屬性為ok的圖片,然后向定位上層div元素 |
| child | |||
| ancestor | 所有上層節點 | ||
| descendant | 所有下層節點 | ||
| following | 當前節點之后所有節點 | ||
| following-sibling | 后面所有同級節點 | ||
| preceding | 當前節點之前所有節點 | ||
| preceding-sibling | 前面所有同級節點 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/162983.html
標籤:Java
上一篇:【5G之道】第五章:物理傳輸資源
