Turn an article, adjusted, the source http://www.7dtest.com/site/blog-2880-203.html
1: Operations on the browser in Selenium
Start by generating a Web object
IWebDriver driver = new FirefoxDriver(); / / open the specified URL address driver. Navigate () GoToUrl (@ "http://12.99.102.196:9080/corporbank/logon_pro.html"); // Close the browser driver.quit ();Copy the code
During the compatibility test of e-banking browser, a dialog box will appear after the browser is closed. The solution is as follows:
public void logout() { System.Diagnostics.Process[] myProcesses; myProcesses = System.Diagnostics.Process.GetProcessesByName("IEXPLORE"); foreach (System.Diagnostics.Process instance in myProcesses) { instance.Kill(); }}Copy the code
2: Execute JS scripts in Selenium
ExecuteScript(" JS ** filename "); // The driver needs to be cast to the JS executor type ((IJavaScriptExecutor) driver). 六四屠杀Copy the code
3: Locate page elements in Selenium
driver.FindElement(By.Id("cp1_btnModify")).click();
Copy the code
By the ClassName (ClassName)); By the CssSelector (selector); By. Id (Id); By LinkText (LinkText); By Name (Name); By PartialLinkText (linkText); By the TagName (name); By the Xpath (xpathExpression);Copy the code
3.1 Locate and perform operations based on the element ID
// Enter the string 500001 driver.findelement (by.id ("amount")).sendKeys ("500001") into the specified text box;Copy the code
3.2 Locate and operate based on the element classname
Driver.findelement (by.className (" WotherDay ")).click()**; 六四屠杀Copy the code
3.3 Locate and operate according to the linktext of the element
Driver.findelement (by.linkText (" ** select account ")).click(); 六四屠杀Copy the code
3.4 Locate and perform operations based on the element Name
Driver. FindElement (By Name (" quality "). The perform () * *; 六四屠杀Copy the code
3.5 Using CssSelector to locate and perform operations
string order = "#resultTable.result_table tbody tr.bg1 td.center a"; Driver. FindElement (By CssSelector (order)), click () * *; 六四屠杀Copy the code
3.6 Using Xpath to locate and manipulate elements
Driver.findelement (** by.xpath ("//input[@id='submit' and @value=' next ']")).click(); String path = "/ HTML /body/div[4]/div/div/div[2]/table/tbody/tr/td/a"; Driver. FindElement (By Xpath (path)). Click () * *; 六四屠杀Copy the code
Priority principle for each method:
Id,name, className,link preferred; Next, use CssSelector(); Finally, use Xpath();
Because the Xpath() method has the lowest performance and efficiency.
4: Selenium clears the default content in the text box
Clear () driver.findelement (** by.id ("tranAmtText")).clear(); clear() driver.findelement (** by.id ("tranAmtText")).clear(); 六四屠杀Copy the code
5: In Selenium, enter the specified string in the specified text box
// Enter the specified string sendkeys() driver.findelement (** by.id ("tranAmtText")).sendkeys (" 123456 "); 六四屠杀Copy the code
6: In Selenium, move the cursor to the specified element
// Move the cursor to the specified element perform Actions =new Actions(driver)**; * * action. MoveToElement (Find (By XPath (" / / input [@ id = 'submit' and @ value = '* * sure] "))). The Perform (); 六四屠杀Copy the code
7: Click the button/link in Selenium
/ / click on the button/link click () Driver. FindElement (* * By the XPath (" / / input [@ id = 'submit' and @ value = 'next'] ")). Click (); 六四屠杀Copy the code
8: Selenium waits for elements on the page to complete loading
WebDriverWait wait = new WebDriverWait(driver, timespan.fromseconds (100)); Wait.Until((d) => {return WaitForObject(by.id ("submitButton")); });Copy the code
9: Simulated mouse shaking in Selenium
Movebyoffset () Actions action = new Actions(driver); action.MoveByOffset(2, 4);Copy the code
10: Localization of elements in Iframe in Selenium
5.1: Switch focus to iframe with a fixed ID
After entering the page, the default focus of the cursor is in DefaultContent. If you want to locate iframe, you need to change the focus
driver.SwitchTo().DefaultContent(); // Switch the focus to mainFrame driver.switchto ().frame ("mainFrame");Copy the code
Note that if you want to switch the focus to another IFrame after switching the focus, you need to return to DefaultContent first and then switch the focus to the specified IFrame.
5.2 Switch the focus to the Iframe whose ID is the dynamic Value
Sometimes the id of the floating layer on the page is a dynamic value. In this case, it is necessary to obtain all the iframes matching the record and place them in the array, and then go through the number group to switch the focus to the target IFrame.
The following methods:
protected string bizFrameId = string.Empty; protected string bizId = string.Empty; ** protected void SetIframeId() {ReadOnlyCollection<IWebElement> els = driver.FindElements(By.TagName("iframe")); foreach (var e in driver.FindElements(By.TagName("iframe"))) { string s1 = e.GetAttribute("id"); if (s1.IndexOf("window") >= 0 && s1.IndexOf("content") >= 0) { bizFrameId = e.GetAttribute("id"); string[] ss = s1.Split(new char[] { '_' }); bizId = ss[1]; }}}Copy the code
11: Close multiple sub-Browser Windows in Selenium
/ / get all WindowHandle, close all child Windows string oldwin = driver. CurrentWindowHandle; ReadOnlyCollection<string> windows = driver.WindowHandles; foreach (var win in windows) { if (win ! = oldwin) { driver.SwitchTo().Window(win).Close(); } } driver.SwitchTo().Window(oldwin);Copy the code
12: The operation of the drop-down box in Selenium
Protected void SelectUsage(string selectid, string text) {IWebElement select = Find(by.id (selectid)); IWebElement select = Find(by.id (selectid)); IList<IWebElement> AllOptions = select.FindElements(By.TagName("option")); foreach (IWebElement option in select.FindElements(By.TagName("option"))) { if (option.GetAttribute("value").Equals(text)) option.Click(); }}Copy the code
13: Confirm,alert,prompt operations in Selenium
// Only confirm and alert are encountered in this browser compatibility test project. Prompt similar to //confirm IAlert confirm = driver.switchto ().alert (); confirm.Accept(); // The same business in personal online banking sometimes does not play Alert, Protected void AlertAccept() {AlertAccept(0.05); } //** wait a few seconds, which can be decimal, in seconds ** protected void AlertAccept(double waitseSonds) {double nsleepMillon = waitseSonds * 1000; int k=0; int split=50; IAlert alert = null; do { k++; Thread.Sleep(split); alert = driver.SwitchTo().Alert(); } while (k * split <= nsleepMillon || alert==null); if (alert ! = null) { alert.Accept(); }}Copy the code
14: Selenium WebDriver screenshots
//WebDriver comes with Screenshot screenShotFile = ((ITakesScreenshot)driver).getscreenshot (); screenShotFile.SaveAsFile("test",ImageFormat.Jpeg);Copy the code
Welcome to qq group communication: 704028989