31 Ağustos 2018 Cuma

selenium codes


1) Installation
Open visual studio, go to Tools -> Nuget Package Manager -> Console and insert
Code:
Install-Package Selenium.WebDriver
2) Basics
Profiles: they define the preferences, as the use of a proxy or an user agent or even the volume
Code:
var profile = new FirefoxProfile();
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.http", ip);
profile.SetPreference("network.proxy.http_port", port);
profile.SetPreference("general.useragent.override", "ebin useragent");
profile.SetPreference("media.volume_scale", "0.0");
IWebDriver: the driver is the browser module that we are using so for example
Code:
IWebDriver driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("http://website.com");
IWebElement: it is an element of the dom page you can use as a node, example
Code:
IWebElement e = w.FindElement(By.Id("mydiv"));
3) Navigation through nodes
Selenium can find an element or multiple elements on the page by many things, IDs, classnames, names, tags, etc...but you will get an exception if this element is not there already! Suppose you have a page which makes a redirect, you will need to wait for that element to show up in order to get it so we have to implement a basic listener
Code:
bool exc = true;
while (exc)
{
   try
     {
        IWebElement e = driver.FindElement(By.Id("mydiv"));
        exc = false;
        //ETC.....
     }
      catch (NoSuchElementException) { }
}
This does not apply to collections, each IWebElement can be used as a node to search elements inside.
If you want to wait in general you can use
Code:
driver.Manage().Timeouts().ImplicitlyWait(timeSpan);
You can even access the page source through driver.PageSource

4)Fulfilling form data and windows
You can easily input form data by using SendKeys() and Submit() methods
Code:
driver.FindElement(By.Id("username")).SendKeys(username);
driver.FindElement(By.Id("password")).SendKeys(password);
driver.FindElement(By.Id("login_form")).Submit();
Even if I suggest to just use .Click() on the submit button due to internal delays

To handle the focus and the windows of the browser you can use the SwitchTo() method which returns an ITargetLocator. For example you can set it to an iframe with .Frame() or to another window with .Window(). Examples:
Code:
//goes to first window
driver.SwitchTo().Window(driver.WindowHandles[0]);
//accepts alert
driver.SwitchTo().Alert().Accept();
To get the current window you can use driver.CurrentWindowHandle and for all driver.WindowHandles

5)Other features
Of course, we can execute javascript as well! By using IJavaScriptExecutor
Code:
(driver as IJavaScriptExecutor).ExecuteScript("alert(1);alert(2);alert(3);");
Or handle cookies
Code:
driver.Manage().Cookies.AddCookie(new Cookie("lol","hello","/","domain.com",new DateTime(2020,10,20)));
var cookies = driver.Manage().Cookies.AllCookies;
driver.Manage().Cookies.DeleteAllCookies();
And last but not least, you can even take screenshots with ITakesScreenshot!
Code:
(driver as ITakesScreenshot).GetScreenshot().SaveAsFile("lol.png",System.Drawing.Imaging.ImageFormat.Png);

If you need to hide the whole process you can just use ShowWindow with SW_HIDE
Code:
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
//...
ShowWindow(Process.GetProcessesByName("firefox")[0].MainWindowHandle, 0);
I put effort on this I really hope you appreciate!

Hiç yorum yok:

Yorum Gönder