Selenium API(C#)
9128 단어 selenium
1 Fetching a Page
driver.Url = "http://www.google.com";
2 Locating UI Elements (WebElements)
By ID
This is the most efficient and preferred way to locate an element. Common pitfalls that UI developers make is having non-unique id’s on a page or auto-generating the id, both should be avoided. A class on an html element is more appropriate than an auto-generated id.
Example of how to find an element that looks like this:
<div id="coolestWidgetEvah">...</div>
IWebElement element = driver.FindElement(By.Id("coolestWidgetEvah"));
By Class Name
“Class” in this case refers to the attribute on the DOM element. Often in practical use there are many DOM elements with the same class name, thus finding multiple elements becomes the more practical option over finding the first element.
Example of how to find an element that looks like this:
Cheddar
Gouda
IList cheeses = driver.FindElements(By.ClassName("cheese"));
By Tag Name
The DOM Tag Name of the element.
Example of how to find an element that looks like this:
By Link Text
Find the link element with matching visible text.
Example of how to find an element that looks like this:
By CSS
Like the name implies it is a locator strategy by css. Native browser support is used by default, so please refer to w3c css selectorsfor a list of generally available css selectors. If a browser does not have native support for css queries, then Sizzle is used. IE 6,7 and FF3.0 currently use Sizzle as the css query engine.
Beware that not all browsers were created equal, some css that might work in one version may not work in another.
Example of to find the cheese below:
By XPATH
At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.
Driver
Tag and Attribute Name
Attribute Values
Native XPath Support
HtmlUnit Driver
Lower-cased
As they appear in the HTML
Yes
Internet Explorer Driver
Lower-cased
As they appear in the HTML
No
Firefox Driver
Case insensitive
As they appear in the HTML
Yes
This is a little abstract, so for the following piece of HTML:
Using JavaScript
You can execute arbitrary javascript to find an element and as long as you return a DOM Element, it will be automatically converted to a WebElement object.
Simple example on a page that has jQuery loaded:
IList
By Tag Name
The DOM Tag Name of the element.
Example of how to find an element that looks like this:
<iframe src="..."></iframe>
IWebElement frame = driver.FindElement(By.TagName("iframe"));
By Link Text
Find the link element with matching visible text.
Example of how to find an element that looks like this:
<a href="http://www.google.com/search?q=cheese">cheese</a>
IWebElement cheese = driver.FindElement(By.LinkText("cheese"));
By CSS
Like the name implies it is a locator strategy by css. Native browser support is used by default, so please refer to w3c css selectors
Beware that not all browsers were created equal, some css that might work in one version may not work in another.
Example of to find the cheese below:
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span
IWebElement cheese = driver.FindElement(By.CssSelector("#food span.dairy.aged"));
By XPATH
At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.
Driver
Tag and Attribute Name
Attribute Values
Native XPath Support
HtmlUnit Driver
Lower-cased
As they appear in the HTML
Yes
Internet Explorer Driver
Lower-cased
As they appear in the HTML
No
Firefox Driver
Case insensitive
As they appear in the HTML
Yes
This is a little abstract, so for the following piece of HTML:
<input type="text" name="example" />
<INPUT type="text" name="other" />
IList<IWebElement> inputs = driver.FindElements(By.XPath("//input"));
Using JavaScript
You can execute arbitrary javascript to find an element and as long as you return a DOM Element, it will be automatically converted to a WebElement object.
Simple example on a page that has jQuery loaded:
IWebElement element = (IWebElement) ((IJavaScriptExecutor)driver).ExecuteScript("return $('.cheese')[0]");
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WDM(WebDriverManager) 시작하기자동화 테스트(브라우저 자동화)에 대해 이야기할 때마다 몇 가지 사항이 있어야 합니다. 브라우저(시스템 경로에 있어야 함). 드라이버 실행 파일(시스템 경로에 있어야 함), 드라이버 실행 파일 버전은 브라우저 버전과...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.