第3章 Selenium的命令

合集下载

Selenium 命令列表

Selenium 命令列表

Selenium 命令列表一、Commands (命令)Action对当前状态进行操作失败时,停止测试Assertion校验是否有产生正确的值Element Locators指定HTML中的某元素Patterns用于模式匹配1. Element Locators (元素定位器)id=idid locator 指定HTML中的唯一id的元素name=namename locator指定HTML中相同name的元素中的第一个元素identifier=ididentifier locator 首先查找HTML是否存在该id的元素, 若不存在,查找第一个该name的元素dom=javascriptExpressiondom locator用JavaScript表达式来定位HTML中的元素,注意必须要以"document"开头例如:dom=document.forms['myForm'].myDropdowndom=document.images[56]xpath=xpathExpressionxpath locator用XPath 表达式来定位HTML中的元素,必须注意要以"//"开头例如:xpath=//img[@alt='The image alt text']xpath=//table[@id='table1']//tr[4]/td[2]link=textPatternlink locator 用link来选择HTML中的连接或锚元素例如:link=The link text在没有locator前序的情况下Without a locator prefix, Selenium uses:如果以"document."开头,则默认是使用dom locator,如果是以"//"开头,则默认使用xpath locator,其余情况均认作identifier locator2. String Matching Patterns (字符串匹配模式)glob:patthernglob模式,用通配符"*"代表任意长度字符,"?"代表一个字符regexp:regexp正则表达式模式,用JavaScript正则表达式的形式匹配字符串exact:string精确匹配模式,精确匹配整个字符串,不能用通配符在没有指定字符串匹配前序的时候,selenium 默认使用golb 匹配模式3. Select Option Specifiers (Select选项指定器)label=labelPattern通过匹配选项中的文本指定选项例如:label=regexp:^[Oo]thervalue=valuePattern通过匹配选项中的值指定选项例如:value=otherid=id通过匹配选项的id指定选项例如: id=option1index=index通过匹配选项的序号指定选项,序号从0开始例如:index=2在没有选项选择前序的情况下,默认是匹配选项的文本二、Actions描述了用户所会作出的操作。

python selenium使用例子

python selenium使用例子

python selenium使用例子使用Python和Selenium进行Web自动化测试可帮助开发人员提供高质量的软件。

Selenium是一个开源的自动化测试框架,它可以模拟用户在网页上的操作,并对网页元素进行交互。

在本文中,我们将使用中括号主题,来展示如何使用Python和Selenium自动化测试框架。

1. 安装和配置Selenium首先,我们需要在Python环境中安装和配置Selenium。

可以使用pip命令来安装Selenium:`pip install selenium`。

安装完成后,需要下载并安装浏览器驱动程序以供Selenium使用,根据你使用的浏览器选择相应的驱动程序。

2. 导入Selenium库安装成功后,我们需要在Python脚本中导入Selenium库:`import selenium`。

这将使我们能够使用Selenium提供的函数和方法来控制浏览器。

3. 创建一个浏览器实例使用Selenium,我们可以模拟用户在浏览器上的操作。

为此,我们首先需要创建一个浏览器实例,例如,Google Chrome:`from selenium import webdriver`,`driver = webdriver.Chrome()`。

这将创建一个Chrome浏览器实例,我们将使用它来进行后续的操作。

4. 打开一个网页一旦有了浏览器实例,我们就可以使用`get()`方法来打开一个网页:`driver.get("5. 寻找和交互网页元素Selenium的一个重要功能是能够寻找和交互网页上的元素。

例如,我们可以使用`find_element_by_xpath()`方法通过XPath来定位一个元素:`element = driver.find_element_by_xpath("/html/body/div[1]/div[2]/form/input[1]")`。

Selenium自动化测试工具使用指南

Selenium自动化测试工具使用指南

Selenium自动化测试工具使用指南第一章:引言自动化测试是现代软件开发中不可或缺的一环。

它能够有效地减少测试用例的执行时间,提高测试的准确性和一致性。

Selenium 自动化测试工具是业界常用的自动化测试框架之一,通过模拟用户操作浏览器,可以对Web应用程序进行自动化功能测试。

本文将深入介绍Selenium的各个组成部分及其使用方法,帮助读者全面了解和应用这一工具。

第二章:Selenium基础知识2.1 Selenium概述Selenium是一个开源的自动化测试工具,支持多种浏览器和操作系统。

它包含了不同语言的客户端库,如Java、Python、C#等,可以根据不同需求选择合适的语言进行自动化测试开发。

2.2 Selenium组成部分Selenium主要由三个组成部分构成:- Selenium WebDriver:用于模拟用户在浏览器上的操作,如点击、输入、下拉等,支持多种浏览器和操作系统。

- Selenium IDE:一个Firefox插件,用于录制和回放用户在浏览器上的操作,适用于简单的测试任务。

- Selenium Grid:用于在多台机器上并行执行测试用例,可提高测试效率。

第三章:使用Selenium WebDriver3.1 Selenium WebDriver安装与配置首先,我们需要下载并安装Selenium WebDriver的驱动程序,根据被测试的浏览器进行选择。

然后,将驱动程序的路径添加到系统环境变量中,以便Selenium能够找到并使用它。

3.2 编写第一个Selenium测试用例在开始编写测试用例之前,我们需要先准备好测试环境,包括所需的浏览器和被测网站。

然后,通过编写具体的代码,使用Selenium WebDriver的API模拟用户的操作,如打开网页、输入表单、点击按钮等。

3.3 元素定位Selenium WebDriver提供了多种方式来定位网页上的元素,包括通过ID、Name、XPath等唯一标识符进行定位。

selenium语句

selenium语句

Selenium是一个用于自动化web应用程序测试的工具,它支持多种编程语言,包括Python、Java、C#等。

以下是Python中使用Selenium的一些常见语句:1. 导入Selenium库:```pythonfrom selenium import webdriver```2. 启动浏览器并打开指定网页:```pythondriver = webdriver.Chrome() # 使用Chrome浏览器driver.get # 打开指定网页```3. 定位元素:```pythonelement = driver.find_element_by_id("element_id") # 通过ID定位元素element = driver.find_element_by_name("element_name") # 通过name定位元素element = driver.find_element_by_xpath("//tagname[@attribute='value']") # 通过XPath定位元素```4. 操作元素:```pythonelement.click() # 点击元素element.send_keys("text") # 在元素中输入文本```5. 关闭浏览器:```pythondriver.quit() # 关闭浏览器窗口并退出Selenium会话```这只是Selenium的一些基本语句,还有更多高级功能和用法,具体可以参考Selenium官方文档。

python菜鸟selenium用法

python菜鸟selenium用法

Selenium 是一个用于自动化浏览器的工具,常用于网页测试和爬虫等场景。

以下
是一个简单的 Selenium 示例,展示如何使用 Selenium 进行基本的网页操作。

首先,确保你已经安装了 Selenium:
然后,你需要下载对应浏览器的驱动。

不同浏览器需要不同的驱动,比如 Chrome
需要 ChromeDriver,Firefox 需要 GeckoDriver。

你可以在官网下载并配置。

下面是一个简单的 Python 示例,演示如何使用 Selenium 打开浏览器,访问网页,
以及进行一些基本的操作:
请注意,这只是 Selenium 的一个简单示例。

Selenium 还支持很多高级功能,比如处理弹窗、截图、模拟鼠标操作等。

Selenium常用命令

Selenium常用命令

Selenium常⽤命令openopen(url)- 在浏览器中打开URL,可以接受相对和绝对路径两种形式typetype(inputLocator, value)- 模拟⼈⼿的输⼊过程,往指定的input中输⼊值- 也适合给复选和单选框赋值clickclick(elementLocator)- 点击连接,按钮,复选和单选框- 如果点击后需要等待响应,则⽤"clickAndWait"- 如果是需要经过JavaScript的alert或confirm对话框后才能继续操作,则需要调⽤verify或assert来告诉Selenium你期望对对话框进⾏什么操作。

goBack()模拟点击浏览器的后退按钮close()模拟点击浏览器关闭按钮 select select(dropDownLocator, optionSpecifier) - 根据optionSpecifier选项选择器来选择⼀个下拉菜单选项 - 如果有多于⼀个选择器的时候,如在⽤通配符模式,如"f*b*",或者超过⼀个选项有相同的⽂本或值,则会选择第⼀个匹配到的值元素的定位以及操作 id name class name link text partial link text tag name css selector xpath通过元素的ID定位元素:findElement(By.id("ele"));通过元素的名称定位元素:findElement(("ele"));通过元素的html中的位置定位元素:findElement(By.xpath("ele"));通过元素的标签名称定位元素:findElement(By.tagName("ele"));通过元素的链接名称定位元素:findElement(By.LinkText("ele"));通过元素的类名定位元素:findElement(By.className("ele"));通过元素的css定位元素:findElement(By.cssSelector("ele");通过元素的部分链接名称定位元素:findElement(By.pareialLinkText("ele"));对ID为ele的元素进⾏点击操作:driver.findElement(By.id(ele));对ID为ele的元素进⾏发送字符操作:driver.findElement(By.id(ele).sendKeys("123456"));操作与获取页⾯元素点击:element.click()填写内容:element.SendKeys()清空:element.clear()提交:element.submit()获取标题:driver.getTitle()获取Url:driver.getCurrentUrl()获取⽂本:driver.getText()存储勾选情况:element.isSelected()验证元素是否可展⽰:element.isDisplayed()获取标签名称:element.getTagName()获取指定属性的值:element.getAttribute()存储可编辑状态:element.isEnabled()浏览器操作打开⽹页:driver.navigate().to("");浏览器最⼤化:driver.manage().window().maximize()前进、后退:navigation.back() navigation.forward()刷新:navigation.refresh()对于普通键盘,使⽤ sendKeys(keysToSend) 就可以实现,⽐如按键 TAB、Backspace 等Actions action = new Actions(driver);action.sendKeys(Keys.TAB);// 模拟按下并释放 TAB 键action.sendKeys(Keys.SPACE);// 模拟按下并释放空格键 /*** 针对某个元素发出某个键盘的按键操作,或者是输⼊操作,⽐如在 input 框中输⼊某个字符也可以使⽤这个⽅法。

Selenium 命令详解

Selenium 命令详解

public class DefaultSelenium : ISelenium{// Fieldsprotected ICommandProcessor commandProcessor;// Methodspublic DefaultSelenium(ICommandProcessor processor);public DefaultSelenium(string serverHost, int serverPort, string browserString, string browserURL);public void AddLocationStrategy(string strategyName, string functionDefinition);public void AddScript(string scriptContent, string scriptTagId);public void AddSelection(string locator, string optionLocator);public void AllowNativeXpath(string allow);public void AltKeyDown();public void AltKeyUp();public void AnswerOnNextPrompt(string answer);public void AssignId(string locator, string identifier);public void AttachFile(string fieldLocator, string fileLocator);public void CaptureEntirePageScreenshot(string filename, string kwargs);public string CaptureEntirePageScreenshotToString(string kwargs);public void CaptureScreenshot(string filename);public string CaptureScreenshotToString();public void Check(string locator); //checkbox.radiopublic void ChooseCancelOnNextConfirmation();public void ChooseOkOnNextConfirmation();public void Click(string locator); //链接,多选框,单选框public void ClickAt(string locator, string coordString);public void Close();public void ContextMenu(string locator);public void ContextMenuAt(string locator, string coordString);public void ControlKeyDown();public void ControlKeyUp();public void CreateCookie(string nameValuePair, string optionsString);public void DeleteAllVisibleCookies();public void DeleteCookie(string name, string optionsString);public void DeselectPopUp();public void DoubleClick(string locator);public void DoubleClickAt(string locator, string coordString);public void DragAndDrop(string locator, string movementsString);public void DragAndDropToObject(string locatorOfObjectToBeDragged, string locatorOfDragDestinationObject);public void Dragdrop(string locator, string movementsString);public void FireEvent(string locator, string eventName);public void Focus(string locator);public string GetAlert();public string[] GetAllButtons();public string[] GetAllFields();public string[] GetAllLinks();public string[] GetAllWindowIds();public string[] GetAllWindowNames();public string[] GetAllWindowTitles(); //测试用过了public string GetAttribute(string attributeLocator);public string[] GetAttributeFromAllWindows(string attributeName);public string GetBodyText();public string GetConfirmation();public string GetCookie();public string GetCookieByName(string name);public decimal GetCursorPosition(string locator);public decimal GetElementHeight(string locator);public decimal GetElementIndex(string locator);public decimal GetElementPositionLeft(string locator);public decimal GetElementPositionTop(string locator);public decimal GetElementWidth(string locator);public string GetEval(string script);public string GetExpression(string expression);public string GetHtmlSource();public string GetLocation();public decimal GetMouseSpeed();public string GetPrompt();public string GetSelectedId(string selectLocator);public string[] GetSelectedIds(string selectLocator);public string GetSelectedIndex(string selectLocator);public string[] GetSelectedIndexes(string selectLocator);public string GetSelectedLabel(string selectLocator);public string[] GetSelectedLabels(string selectLocator);public string GetSelectedValue(string selectLocator);public string[] GetSelectedValues(string selectLocator);public string[] GetSelectOptions(string selectLocator);public string GetSpeed();public string GetTable(string tableCellAddress);public string GetText(string locator);public string GetTitle();public string GetValue(string locator);public bool GetWhetherThisFrameMatchFrameExpression(string currentFrameString, string target);public bool GetWhetherThisWindowMatchWindowExpression(string currentWindowString,string target);public decimal GetXpathCount(string xpath);public void GoBack();public void Highlight(string locator);public void IgnoreAttributesWithoutValue(string ignore);public bool IsAlertPresent();public bool IsChecked(string locator);public bool IsConfirmationPresent();public bool IsCookiePresent(string name);public bool IsEditable(string locator);public bool IsElementPresent(string locator);public bool IsOrdered(string locator1, string locator2);public bool IsPromptPresent();public bool IsSomethingSelected(string selectLocator);public bool IsTextPresent(string pattern);public bool IsVisible(string locator);public void KeyDown(string locator, string keySequence);public void KeyDownNative(string keycode);public void KeyPress(string locator, string keySequence);public void KeyPressNative(string keycode);public void KeyUp(string locator, string keySequence);public void KeyUpNative(string keycode);public void MetaKeyDown();public void MetaKeyUp();public void MouseDown(string locator);public void MouseDownAt(string locator, string coordString);public void MouseDownRight(string locator);public void MouseDownRightAt(string locator, string coordString);public void MouseMove(string locator);public void MouseMoveAt(string locator, string coordString);public void MouseOut(string locator);public void MouseOver(string locator);public void MouseUp(string locator);public void MouseUpAt(string locator, string coordString);public void MouseUpRight(string locator);public void MouseUpRightAt(string locator, string coordString);public void Open(string url);public void OpenWindow(string url, string windowID);public void Refresh();public void RemoveAllSelections(string locator);public void RemoveScript(string scriptTagId);public void RemoveSelection(string locator, string optionLocator);public string RetrieveLastRemoteControlLogs();public void Rollup(string rollupName, string kwargs);public void RunScript(string script); 涉及JS还需再试试public void Select(string selectLocator, string optionLocator);public void SelectFrame(string locator);public void SelectPopUp(string windowID);public void SelectWindow(string windowID);public void SetBrowserLogLevel(string logLevel);public void SetContext(string context);public void SetCursorPosition(string locator, string position);public void SetExtensionJs(string extensionJs);public void SetMouseSpeed(string pixels);public void SetSpeed(string value);public void SetTimeout(string timeout);public void ShiftKeyDown();public void ShiftKeyUp();public void ShutDownSeleniumServer();public void Start();public void Stop();public void Submit(string formLocator);public void Type(string locator, string value);public void TypeKeys(string locator, string value);public void Uncheck(string locator);public void UseXpathLibrary(string libraryName);public void WaitForCondition(string script, string timeout);public void WaitForFrameToLoad(string frameAddress, string timeout);public void WaitForPageToLoad(string timeout);public void WaitForPopUp(string windowID, string timeout);public void WindowFocus();public void WindowMaximize();// Propertiespublic ICommandProcessor Processor { get; }}Expand Methods。

selenium常用方法

selenium常用方法

selenium常用方法Selenium 是一个用于自动化浏览器操作的工具,常用于网页测试和爬虫开发。

它提供了丰富的方法和功能,以下是一些常用的 Selenium 方法:1. WebDriver 的基本方法:- `get(url)`:加载指定的网页。

- `title`:获取当前网页的标题。

- `current_url`:获取当前网页的 URL。

- `back(`:返回上一个网页。

- `forward(`:前进到下一个网页。

- `refresh(`:刷新当前网页。

2.元素定位方法:- `find_element_by_id(id)`:根据元素 ID 定位单个元素。

- `find_elements_by_id(id)`:根据元素 ID 定位多个元素。

- `find_element_by_name(name)`:根据元素名称定位单个元素。

- `find_elements_by_name(name)`:根据元素名称定位多个元素。

- `find_element_by_class_name(class_name)`:根据元素类名定位单个元素。

- `find_elements_by_class_name(class_name)`:根据元素类名定位多个元素。

- `find_element_by_link_text(link_text)`:根据链接文本定位单个元素。

- `find_elements_by_link_text(link_text)`:根据链接文本定位多个元素。

- `find_element_by_partial_link_text(link_text)`:根据链接部分文本定位单个元素。

- `find_elements_by_partial_link_text(link_text)`:根据链接部分文本定位多个元素。

- `find_element_by_xpath(xpath)`:根据 XPath 定位单个元素。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

21
Web应用项目测试
15
Web应用项目测试

captureEntirePageScreenShot(filename,kwagrs) 当前窗口进行截图并保存为PNG文件 highlight(locator) 暂时将指定元素的背景色改变为黄色。有利于调试。 参数: · locator - 一个元素定位器


Echo(message) 打印指定消息到你的Selenese 表的第三个表单元。有利于调试。 参数: · message – 要打印的消息
16
Web应用项目测试
2 Accessor



于检查应用程序的状态并将结果存储在变量中 store(expression,variableName) 该命令是存储表达式的同义词。 参数: · expression – 要存储的值 · variableName – 用于存储结果的变量名 storeTitle (variableName) 存放当前网页的标题 storeLocation (variableName) 存放当前网页的URL
17
Web应用项目测试



storeBodyText(variableName) 获取页面上所有文本。 Returns: 页面上所有文本 storeHtmlSource(variableName) 返回”html‖标签间的整个HTML源代码。 Returns: 整个HTML源代码 storeEditable(locator,variableName) 判定指定的输入元素是否为可编辑,且ie没有被禁用。如果指定元素不为 一个可输入元素,该函数将失败。 参数: · locator – 一个元素定位器 · variableName - 用于存储结果的变量名。 Returns: 如果输入元素可编辑返回true,否则返回false

按下control键并保持其按下状态,直到doControlUp()被调用或一个新的页面被加载。 释放control键 按下shift键,并保持按下状态,直到doShiftUp()被调用或一个新的页面被加载。 释放shift键

controlKeyUp()


shiftKeyDown()


shiftKeyUp()
19
Web应用项目测试
3.1验证网页标题

assertTitle(pattern) assertNotTitle(pattern) verifyTitle(pattern) verifyNotTitle(pattern) waitForTitle(pattern) waitForTitle(pattern)
Web应用项目测试

doubleClick(locator)


双击一个链接、按钮、多选框或单选框。如果该双击事件导致了新的页面 加载(如同链接通常所作的),将调用waitForPageToLoad。 参数: · locator - 一个元素定位器

doubleClickAt(locator,coordString)
20
Web应用项目测试
3.2验证网页URL

assertLocation(pattern) assertNotLocation (pattern) verifyLocation (pattern) verifyNotLocation (pattern) waitForLocation (pattern) waitForLocation (pattern)


模拟用户从指定元素上移开鼠标指针。 参数: · locator - 一个元素定位器

mouseOver(locator)

模拟用户鼠标滑过指定元素。 参数: · locator - 一个元素定位器
13
Web应用项目测试
4)设置类操作

setSpeed(value)


设定执行速度(也就是说,设定将要执行的每条selenium操作间的毫秒延 迟间隔长度)。默认情况下,没有延迟,也就是延迟为0毫秒。 参数: · value – 在操作后的暂停毫秒数
1)、浏览器的操作

Open(url)

参数:Target 当Target为空时,将打开Base URL中的页面 当Target不为空时,将打Base Url+Taget页面 当Target以http://或https://时,直接打开Target网址

goBack()

相当单击浏览器上的后退按钮

参数: · selectLocator – 一个用于识别下拉菜单的元素定位器 · optionLocator – 一个选项选择器(默认为标签)
8
Web应用项目测试

check(locator)

勾选一个关联性按钮(checkbox/radio) 参数: · locator – 一个元素定位器 取消选中一个关联性按钮(checkbox/radio) 参数: · locator – 一个元素定位器

setTimeout(timeout)



指定Selenium等待动作完成的等待时间。 需要等待的动作包括”open‖和”waitFor*‖。 默认超时为30秒。 参数: · timeout – 以毫秒为单位,超过后该命令将返回错误。
14
Web应用项目测试
5)、测试控制、调试类操作
Web应用项目测试
第三章 Selenium的命令
Web应用项目测试
本章内容:


Actions Accessor Assertion
Element Locators

String Matching Patterns
2
Web应用项目测试
1、Actions
一般用于操作应用程序的状
3
Web应用项目测试
刷新
4

Refresh()


Close() 关闭浏览器窗口
Web应用项目测试
2)、界面元素的基本操作

Type(locator,value)

用于input类型的元素中Fra bibliotek入值 参数:
Target 元素的定位表达式 Value 要输入的值

typeKeys(locator,value)

模拟键盘敲击事件,一个一个地输入字符。
5
Web应用项目测试


Click(locator) 单击 如果是加载页面,可在后面加waitForPageToLoad 或使用clickAndWait clickAt(locator,coordString) 点击一个链接、按钮、多选框或单选框。如果该点击事 件导致了新的页面加载(如同链接通常所作的),将调用 waitForPageToLoad。 参数: · locator -一个元素定位器 · coordString – 指定由定位器返回的鼠标事件相关联的 元素x,y 坐标(也就是 – 10,20) 6

11
Web应用项目测试

mouseDown(locator) 模拟用户在指定元素上按下鼠标按钮左键 参数: · locator - 一个元素定位器 mouseDownAt(locator,coordString) 模拟用户在指定位置上按下鼠标按钮左键 参数: · locator - 一个元素定位器 · coordString -指定由定位器返回的鼠标事件相关联的元素x,y 坐标(也就是 – 10,20) mouseUp(locator)

keyPress(locator,keySequence)


keyUp(locator,keySequence)

10
Web应用项目测试



altKeyDown() 按下alt键并保持其按下状态,直到doAltUp()被调用或一个新的页面被加载。 altKeyUp() 释放alt键 controlKeyDown()


双击一个链接、按钮、多选框或单选框。如果该双击事件导致了新的页面 加载(如同链接通常所作的),将调用waitForPageToLoad。 参数: · locator - 一个元素定位器 · coordString – 指定由定位器返回的鼠标事件相关联的元素x,y 坐标(也就 是 – 10,20)
18
Web应用项目测试
3Assertion

可以验证应用程序的状态是否同所期望的相一致。 例如包括”确认页面标题为X‖和”验证该复选框是否被勾选”。 Selenium断言可以被用于三种模式: ―assert‖, ―verify‖,和‖waitfor‖。 例如‖assertText‖,‖verifyText‖,及‖waitforText‖。 当”assert‖失败时,该测试将终止。当”verify‖失败时,该测试将 继续执行,并将错误记入日志。这就允许了通过单条”assert‖确 保应用程序在正确的页面上,而通过一系列的”verify‖断言测试表 单上的区域值,标签等. ―waitfor‖命令用于等待某些条件变为真。如果该条件已经为真,他 们将立即成功执行。反之,如果该条件不为真,则将失败并暂停测 试,直到超过当前所设定的超时时间(参照后面的setTimeout动作) 。

uncheck(locator)

9
Web应用项目测试
3)键盘鼠标模拟操作

keyDown(locator,keySequence)
相关文档
最新文档