Open visual studio, go to Tools -> Nuget Package Manager -> Console and insert
Code:
Install-Package Selenium.WebDriver
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");
Code:
IWebDriver driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("http://website.com");
Code:
IWebElement e = w.FindElement(By.Id("mydiv"));
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) { }
}
If you want to wait in general you can use
Code:
driver.Manage().Timeouts().ImplicitlyWait(timeSpan);
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();
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();
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);");
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();
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);
Hiç yorum yok:
Yorum Gönder