Selenium WebDriver

We need browsers to test web applications. Selenium automates browser and helps us in automation of web application testing across different browsers. Selenium API has provided many classes and interfaces to work with different types of browsers and HTML elements.

我们需要浏览器来测试Web应用程序。 Selenium使浏览器自动化,并帮助我们跨不同浏览器自动化Web应用程序测试。 Selenium API提供了许多类和接口,可用于不同类型的浏览器和HTML元素。

什么是Selenium WebDriver接口? (What is Selenium WebDriver Interface?)

Selenium WebDriver is an interface that defines a set of methods. However, implementation is provided by the browser specific classes. Some of the implementation classes are AndroidDriver, ChromeDriver, FirefoxDriver, InternetExplorerDriver, IPhoneDriver, SafariDriver etc.

Selenium WebDriver是定义一组方法的接口。 但是,实现是由浏览器特定的类提供的。 一些实现类是AndroidDriverChromeDriverFirefoxDriverInternetExplorerDriverIPhoneDriverSafariDriver等。

The WebDriver main functionality is to control the browser. It even helps us to select the HTML page elements and perform operations on them such as click, filling a form fields etc.

WebDriver的主要功能是控制浏览器。 它甚至可以帮助我们选择HTML页面元素并对其进行操作,例如单击,填充表单字段等。

Selenium WebDriver

Selenium WebDriver

Selenium WebDriver

If we want to execute your test cases in a Firefox browser we have to use FirefoxDriver class. Similarly, if we want to execute the test cases in the Chrome browser we have to use ChromeDriver class.

如果要在Firefox浏览器中执行测试用例,则必须使用FirefoxDriver类。 同样,如果要在Chrome浏览器中执行测试用例,则必须使用ChromeDriver类。

Selenium WebDriver方法 (Selenium WebDriver Methods)

SearchContext is the topmost interface in Selenium API which has two methods – findElement() and findElements().

SearchContext是Selenium API中最顶层的接口,具有两种方法-findElement()和findElements()。

Selenium WebDriver interface has many abstract methods like get(String url), quit(), close(), getWindowHandle(), getWindowHandles(), getTitle() etc.

Selenium WebDriver接口具有许多抽象方法,例如get(String url),quit(),close(),getWindowHandle(),getWindowHandles(),getTitle()等。

WebDriver has nested interfaces like Window, Navigation, Timeouts etc. These nested interfaces are used to perform operations like back(), forward() etc.

WebDriver具有嵌套的接口,如WindowNavigationTimeouts等。这些嵌套的接口用于执行诸如back(),forward()等操作。

MethodDescription
get(String url)This method will launch a new browser and opens the given URL in the browser instance.
getWindowHandle()It is used to handle single window i.e. main window. It return type is string. It will returns browser windlw handle from focused browser.
getWindowHandles()It is used to handle multiple windows. It return type is Set. It will returns all handles from all opened browsers by Selenium WebDriver.
close()This command is used to close the current browser window which is currently in focus.
quit()This method will closes all the browsers windows which are currently opened and terminates the WebDriver session.
getTitle()This method is used to retrieve the title of the webpage the user currently working on.
方法 描述
get(字符串URL) 此方法将启动新的浏览器,并在浏览器实例中打开给定的URL。
getWindowHandle() 它用于处理单个窗口,即主窗口。 它的返回类型是字符串。 它将从重点浏览器返回浏览器windlw句柄。
getWindowHandles() 它用于处理多个窗口。 返回类型为Set。 它将通过Selenium WebDriver从所有打开的浏览器返回所有句柄。
关() 此命令用于关闭当前处于焦点的当前浏览器窗口。
退出() 此方法将关闭当前打开的所有浏览器窗口,并终止WebDriver会话。
getTitle() 此方法用于检索用户当前正在处理的网页的标题。

实现WebDriver的类列表 (List of classes implementing WebDriver)

The major implementation classes of WebDriver interface are ChromeDriver, EdgeDriver, FirefoxDriver, InternetExplorerDriver etc. Each driver class corresponds to a browser. We simply create the object of the driver classes and work with them.

WebDriver界面的主要实现类是ChromeDriver,EdgeDriver,FirefoxDriver,InternetExplorerDriver等。每个驱动程序类均对应于一个浏览器。 我们只需创建驱动程序类的对象并使用它们即可。

ClassDescription
ChromeDriverIt helps you to execute Selenium Scripts on Chrome browser.
FirefoxDriverIt helps you to execute Selenium Scripts on Firefox browser.
InternetExplorerDriverIt helps you to execute Selenium Scripts on InternetExplorer browser.
描述
ChromeDriver 它可以帮助您在Chrome浏览器上执行Selenium脚本。
火狐浏览器 它可以帮助您在Firefox浏览器上执行Selenium脚本。
InternetExplorerDriver 它可以帮助您在InternetExplorer浏览器上执行Selenium脚本。

WebElement上的命令列表 (List of Commands on WebElement)

Selenium WebElement represents an HTML element. We can get an instance of WebElement using findElement() method and then perform specific actions such as click, submit etc.

Selenium WebElement表示一个HTML元素。 我们可以使用findElement()方法获取WebElement的实例,然后执行特定的操作,例如单击,提交等。

Some of the commonly used WebElement methods are:

一些常用的WebElement方法是:

CommandDescriptionSyntax
findElement()This method finds the first element within the current web page by using given locator.WebElement element = driverObject.findElement(By.locator(“value”));
sendKeys()This method enters a value in to an Edit Box or Text box.driver.findElement(By.elementLocator(“value”)).sendkeys(“value”);
clear()It clears the Value from an Edit box or Text Box.driverObject.findElement(By.locatorname(“value”)).clear();
click()It clicks an Element (Button, Link, Checkbox) etc.driverObject.findElement(By.ElementLocator(“LocatorValue”)).click();
命令 描述 句法
findElement() 此方法使用给定的定位符在当前网页中查找第一个元素。 WebElement元素= driverObject.findElement(By.locator(“ value”));
sendKeys() 此方法在“编辑框”或“文本框”中输入一个值。 driver.findElement(By.elementLocator(“ value”))。sendkeys(“ value”);
明确() 它从“编辑”框或“文本框”中清除“值”。 driverObject.findElement(By.locatorname(“ value”))。clear();
click() 单击一个元素(按钮,链接,复选框)等。 driverObject.findElement(By.ElementLocator(“ LocatorValue”))。click();

Selenium WebDriver示例–打印网站标题 (Selenium WebDriver Example – Print Website Title)

Let’s look at a simple example of using Selenium WebDriver to invoke Firefox browser and print the title of a website.

让我们看一个使用Selenium WebDriver调用Firefox浏览器并打印网站标题的简单示例。

package com.journaldev.selenium.firefox;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GeckoDriverExample {

	public static void main(String[] args) {
		//specify the location of GeckoDriver for Firefox browser automation
		System.setProperty("webdriver.gecko.driver", "geckodriver");
		WebDriver driver = new FirefoxDriver();
		driver.get("https://journaldev.com");
		String PageTitle = driver.getTitle();
		System.out.println("Page Title is:" + PageTitle);
		driver.close();
	}
}

Output:

输出:

1551941763563	mozrunner::runner	INFO	Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-foreground" "-no-remote" "-profile" "/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/rust_mozprofile.t6ZyMHsrf2bh"
1551941764296	addons.webextension.screenshots@mozilla.org	WARN	Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: resource://pdf.js/
1551941764297	addons.webextension.screenshots@mozilla.org	WARN	Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: about:reader*
Can't find symbol 'GetGraphicsResetStatus'.
1551941765794	Marionette	INFO	Listening on port 61417
1551941765818	Marionette	WARN	TLS certificate errors will be ignored for this session
Mar 07, 2019 12:26:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Page Title is:JournalDev - Java, Java EE, Android, Python, Web Development Tutorials
1551941814652	Marionette	INFO	Stopped listening on port 61417
Selenium WebDriver Example

Selenium WebDriver Example

Selenium WebDriver示例

GitHub Repository. GitHub Repository中检出更多Selenium示例。

Reference: WebDriver GitHub Code

参考: WebDriver GitHub代码

翻译自: https://www.journaldev.com/26429/selenium-webdriver

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.1 selenium webdriver学习(一)------------快速开始 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 selenium webdriver学习(二)————对浏览器的简单操作 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 selenium webdriver学习(三)------------执行js脚本 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.4 selenium webdriver学习(四)------------定位页面元素 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.5 selenium webdriver学习(五)------------iframe的处理 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.6 selenium webdriver学习(六)------------如何得到弹出窗口 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.7 selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框 . . . . . . . . .24 1.8 selenium webdriver学习(八)------------如何操作select下拉框 . . . . . . . . . . . . . . . . . . . . . . . . .27 1.9 selenium webdriver学习(九)------------如何操作cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1.10 selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 . . . . . . . . . . .31 1.11 selenium webdriver学习(十一)------------如何等待页面元素加载完成 . . . . . . . . . . . . . . . . . .33 1.12 selenium webdriver学习(十二)------------如何利用selenium-webdriver截图 . . . . . . . . . . . .38
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值