自动化测试常用操作
1、定位网页上的元素,并存储到一个变量中;2、对变量进行操作,比如点击或输入文字;3、设定页面元素的操作值;
二、定位方法汇总,拉勾IT课小编为大家分析,该元素的技术。
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
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='查询']"));
//上述xpath使用的是绝对路径定位,当页面发生变化时,很容易造成代码失效。
//所以推荐使用相对路径,比如将上述定位改成:"//input[@value='查询']"
//如果定位元素是列表时,下标从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']"));