initialization
//Google Chromeusing ; IWebDriver driver = new ChromeDriver(); //Firefox Browserusing ; IWebDriver driver = new FirefoxDriver(); // PhantomJS browserusing ; IWebDriver driver = new PhantomJSDriver(); // IE browserusing ; IWebDriver driver = new InternetExplorerDriver(); // Edge browserusing ; IWebDriver driver = new EdgeDriver();
Positioning tag method
(("className")); (("css")); (("id")); (("text")); (("name")); (("pText")); (("input")); (("//*[@id='editor']")); // Find multiple elementsIReadOnlyCollection<IWebElement> anchors = (("a")); //Search for another elementvar div = (("div")) .FindElement(("a")); Basic browser operations // Navigate to the page().GoToUrl(@""); // Get the title of the pagestring title = ; // Get the current URLstring url = ; // Get the HTML source of the current pagestring html = ;
Basic elements operation
IWebElement element = (("id")); (); ("someText"); (); (); string innerText = ; bool isEnabled = ; bool isDisplayed = ; bool isSelected = ; IWebElement element = (("id")); SelectElement select = new SelectElement(element); (1); ("Ford"); ("ford"); (); (1); ("Ford"); ("ford"); IWebElement selectedOption = ; IList<IWebElement> allSelected = ; bool isMultipleSelect = ;
Advanced element operation
// Drag and dropIWebElement element = ( ("//*[@id='project']/p[1]/div/div[2]")); Actions move = new Actions(driver); (element, 30, 0).Perform(); // How to check if the element is visible(( ("//*[@id='tve_editor']/div")).Displayed); //Upload fileIWebElement element = (("RadUpload1file0")); String filePath = @"D:\\\"; (filePath); // Scroll focus to controlIWebElement link = (("Previous post")); string js = ("(0, {0});", ); ((IJavaScriptExecutor)driver).ExecuteScript(js); (); //Search screenshots of elementsIWebElement element = (("//*[@id='tve_editor']/div")); var cropArea = new Rectangle(, ); var bitmap = (cropArea, ); (fileName); // Follow the controlsIWebElement link = (("Previous post")); // Wait for visibility of the elementWebDriverWait wait = new WebDriverWait(driver, (30)); ( ( ("//*[@id='tve_editor']/div[2]/div[2]/div/div")));
Advanced browser operation
// Handle JavaScript popup windowsIAlert a = ().Alert(); (); (); // Switch between browser windows or tabsReadOnlyCollection<string> windowHandles = ; string firstTab = (); string lastTab = (); ().Window(lastTab); // History().Back(); ().Refresh(); ().Forward(); // Option 1. (); // Option 2. ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].focus();", link); // Maximize window().(); // Add new cookiesCookie cookie = new ("key", "value"); ().(cookie); // Get all cookiesvar cookies = ().; //Delete cookies by name().("CookieName"); // Delete all cookies().(); //Full screenshotScreenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot(); (@"pathToImage", ); // Wait for the page to fully load through JavaScriptWebDriverWait wait = new WebDriverWait(, (30)); ((x) => { return ((IJavaScriptExecutor)).ExecuteScript( "return ").Equals("complete"); }); // Switch to frame().Frame(1); ().Frame("frameName"); IWebElement element = (("id")); ().Frame(element); // Switch to the default document().DefaultContent();
Advanced browser configuration
// Firefox configuration fileFirefoxProfileManager profileManager = new FirefoxProfileManager(); FirefoxProfile profile = ("HARDDISKUSER"); IWebDriver driver = new FirefoxDriver(profile); // Set up HTTP proxy FirefoxFirefoxProfile firefoxProfile = new FirefoxProfile(); ("", 1); ("", ""); (".http_port", 3239); IWebDriver driver = new FirefoxDriver(firefoxProfile); // Set up HTTP proxy ChromeChromeOptions options = new ChromeOptions(); var proxy = new Proxy(); = ; = false; = = "127.0.0.1:3239"; = proxy; ("ignore-certificate-errors"); IWebDriver driver = new ChromeDriver(options); // Accept all certificates FirefoxFirefoxProfile firefoxProfile = new FirefoxProfile(); = true; = false; IWebDriver driver = new FirefoxDriver(firefoxProfile); // Accept all certificates ChromeDesiredCapabilities capability = (); ("", "C:\\"); (, true); IWebDriver driver = new RemoteWebDriver(capability); // Set Chrome options.ChromeOptions options = new ChromeOptions(); DesiredCapabilities dc = (); (, options); IWebDriver driver = new RemoteWebDriver(dc); // Close JavaScript FirefoxFirefoxProfileManager profileManager = new FirefoxProfileManager(); FirefoxProfile profile = ("HARDDISKUSER"); ("", false); IWebDriver driver = new FirefoxDriver(profile); // Set the default page loading timeout().Timeouts().SetPageLoadTimeout(new TimeSpan(10)); // Use plug-in to start FirefoxFirefoxProfile profile = new FirefoxProfile(); (@"C:\extensionsLocation\"); IWebDriver driver = new FirefoxDriver(profile); // Start Chrome with unpacked extensionChromeOptions options = new ChromeOptions(); ("load-extension=/pathTo/extension"); DesiredCapabilities capabilities = new DesiredCapabilities(); (, options); DesiredCapabilities dc = (); (, options); IWebDriver driver = new RemoteWebDriver(dc); //Start Chrome with compression extensionChromeOptions options = new ChromeOptions(); (("localpathto/")); DesiredCapabilities capabilities = new DesiredCapabilities(); (, options); DesiredCapabilities dc = (); (, options); IWebDriver driver = new RemoteWebDriver(dc); // Change the default file saving locationString downloadFolderPath = @"c:\temp\"; FirefoxProfile profile = new FirefoxProfile(); ("", 2); ("", downloadFolderPath); ("", false); ("", "application/msword, application/binary, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream"); = new FirefoxDriver(profile);
The above is the detailed content of the common operation summary of Selenium WebDriver in C#. For more information about Selenium WebDriver in C#, please pay attention to my other related articles!