简体   繁体   English

Selenium中的WebDriver和WebElement有什么区别?

[英]What is the difference between WebDriver and WebElement in Selenium?

What is the difference between WebDriver and WebElement in Selenium? Selenium中的WebDriver和WebElement有什么区别?

Sample Code:示例代码:

WebDriver driver = new FirefoxDriver();      
driver.get("http://www.google.com");      
WebElement s  = driver.findElement(By.name("q"));      
s.sendKeys("Packt Publishing");      
s.submit();

WebDriver Interface网络驱动程序接口

From Selenium 's perspective, the What is the difference between ChromeDriver and WebDriver in selenium?Selenium的角度来看, Selenium 中ChromeDriver 和 WebDriver什么区别? interface is similar like a agreement which the 3rd party Browser Vendors like Mozilla , Chrome , Internet Explorer , Safari , etc have to adhere and implement the same.界面类似于MozillaChromeInternet ExplorerSafari等第三方浏览器供应商必须遵守和实施的协议。 This would in-turn help the end-users to use the exposed APIs to write a common code and implement the functionalities across all the available browsers without any change.这反过来将帮助最终用户使用公开的 API 编写通用代码并在所有可用浏览器中实现功能,而无需任何更改。


WebDriver driver = new FirefoxDriver(); WebDriver 驱动程序 = 新的 FirefoxDriver();

Through the line of code:通过这行代码:

WebDriver driver = new FirefoxDriver();

We are creating an instance of the WebDriver Interface and casting it to FirefoxDriver class.我们正在创建WebDriver 接口的实例并将其转换FirefoxDriver类。 All the Browser Drivers like FirefoxDriver , ChromeDriver , InternetExplorerDriver , PhantomJSDriver , SafariDriver etc implemented the WebDriver interface (actually the RemoteWebDriver class implements WebDriver Interface and the browser drivers extends RemoteWebDriver ).所有浏览器驱动程序,FirefoxDriverChromeDriverInternetExplorerDriverPhantomJSDriverSafariDriver 等都实现了WebDriver接口(实际上RemoteWebDriver类实现了WebDriver 接口浏览器驱动程序扩展了RemoteWebDriver )。 So if we use WebDriver driver , then we can use the already initialized driver instance (as common object variable) for all browsers we want to automate eg Mozilla, Chrome, InternetExplorer, PhantomJS, Safari.因此,如果我们使用WebDriver driver ,那么我们可以将已经初始化的驱动程序实例(作为公共对象变量)用于我们想要自动化的所有浏览器,例如 Mozilla、Chrome、InternetExplorer、PhantomJS、Safari。

WebDriver driver = new FirefoxDriver();
driver = new ChromeDriver();
driver = new FirefoxDriver();
driver = new SafariDriver();

You can find a detailed discussion in:您可以在以下位置找到详细讨论:


WebElement Interface网页元素界面

From Selenium perspective, WebElement represents an HTML element.Selenium 的角度来看, WebElement代表一个 HTML 元素。 Generally, all the operations to do with interacting with a page will be performed through this interface.通常,与页面交互的所有操作都将通过该接口执行。

A WebElement is an abstraction used to identify the Element nodes and are simply known as elements when it is transported via the protocol, between remote and local ends. WebElement是用于标识Element 节点的抽象,当它通过协议在远程和本地端之间传输时简称为元素 The web element identifier is the string constant expressed as: Web 元素标识符是字符串常量,表示为:

"element-6066-11e4-a52e-4f735466cecf"

You can find a detailed discussion in Values returned by webdrivers您可以在Webdrivers 返回的值中找到详细讨论

Each element has an associated web element reference that uniquely identifies the element across all browsing contexts.每个元素都有一个关联的 Web 元素引用,它在所有浏览上下文中唯一标识该元素。 The web element reference for every element representing the same element must be the same.表示相同元素的每个元素的 Web 元素引用必须相同。 It must be a string, and should be the result of generating a UUID.它必须是一个字符串,并且应该是生成 UUID 的结果。

An ECMAScript Object represents a web element if it has a web element identifier own property.如果 ECMAScript对象具有 Web 元素标识符自己的属性,则它表示一个 Web 元素。

Each browsing context has an associated list of known elements.每个浏览上下文都有一个相关的已知元素列表。 When the browsing context is discarded, the list of known elements is discarded along with it.当浏览上下文被丢弃时,已知元素列表也随之被丢弃。

You can find a detailed discussion in Why return type of findElement(By by) is WebElement?您可以在为什么 findElement(By by) 的返回类型是 WebElement?

Some of the commonly used associated methods are as follows:一些常用的关联方法如下:

  • clear()
  • click()
  • findElement(By by)
  • findElements(By by)
  • getAttribute(java.lang.String name)
  • getCssValue(java.lang.String propertyName)
  • getLocation()
  • getRect()
  • getSize()
  • getTagName()
  • getText()
  • isDisplayed()
  • isEnabled()
  • isSelected()
  • sendKeys(java.lang.CharSequence... keysToSend)
  • submit()

The WebDriver class focuses on driving the browser in a broad sense. WebDriver类侧重于广义上的浏览器驱动。 It loads pages, it switches to different windows/frames, gets the page title etc. Broad actions that aren't specific to an element on the page.它加载页面,切换到不同的窗口/框架,获取页面标题等。不特定于页面元素的广泛操作。

WebElement concentrates on interacting with a specific element that you've located. WebElement专注于与您找到的特定元素进行交互。 Things like:像:

  • Clicking that specific element单击该特定元素
  • Retrieving text and other values from that specific element从该特定元素检索文本和其他值
  • Finding out where that specific element is positioned找出特定元素的位置
  • Sending text to that specific element (like filling an input field)将文本发送到该特定元素(如填充输入字段)

The only real overlap between WebDriver and WebElement are the findElement and findElements methods. WebDriver 和 WebElement 之间唯一真正重叠的是 findElement 和 findElements 方法。 For Webdriver , these methods locate the given By anywhere on the page.对于Webdriver ,这些方法将给定的 By 定位在页面上的任何位置。 For WebElement these methods locate the given By within the context of that element (inside it, generally).对于WebElement,这些方法在该元素的上下文中(通常在其中)定位给定的 By。

Simple answer:简单的答案:

  • The WebDriver focuses on manipulating the browser. WebDriver专注于操纵浏览器。
  • The WebElement is just an Document element object like <button></button> WebElement只是一个文档元素 object,如<button></button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium中定位器和Webelement之间有什么区别? - What is the difference between a locator and a Webelement in Selenium? Selenium WebElement 中的“属性”和“属性”有什么区别? - what is the difference between 'property' and 'attribute' in Selenium WebElement? 硒-WebDriver.findElement()和WebElement.findElement()之间的区别 - Selenium - Difference between WebDriver.findElement() and WebElement.findElement() selenium 中的 ChromeDriver 和 WebDriver 有什么区别? - What is the difference between ChromeDriver and WebDriver in selenium? Selenium Webdriver和SoapUI有什么区别? - What is the difference between Selenium Webdriver and SoapUI? Selenium WebDriver 中的 getText() 和 getAttribute() 有什么区别? - What is the difference between getText() and getAttribute() in Selenium WebDriver? Selenium Webdriver 和 GeckoDriver 有什么区别? - What is the difference between Selenium Webdriver and GeckoDriver? Selenium IDE、Selenium RC 和 Selenium WebDriver 有什么区别? - What is the difference between Selenium IDE, Selenium RC and Selenium WebDriver? Selenium - @FindBy和WebElement.findElement()之间的区别 - Selenium - difference between @FindBy and WebElement.findElement() 扩展Selenium WebDriver WebElement? - Extend Selenium WebDriver WebElement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM