0% found this document useful (0 votes)
62 views

Selenium Methods

The document discusses various methods available in the Selenium WebDriver interface for locating elements, managing browser sessions, and interacting with web pages. It describes methods like FindElement, FindElements, and SwitchTo for finding elements and switching between windows. It also outlines properties and methods on the WebDriver and WebElement objects for tasks like getting page titles, clicking elements, and entering text.

Uploaded by

Abhishek Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Selenium Methods

The document discusses various methods available in the Selenium WebDriver interface for locating elements, managing browser sessions, and interacting with web pages. It describes methods like FindElement, FindElements, and SwitchTo for finding elements and switching between windows. It also outlines properties and methods on the WebDriver and WebElement objects for tasks like getting page titles, clicking elements, and entering text.

Uploaded by

Abhishek Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SearchContext Methods:

1. FindElement : This method searches for an element in the current page based on the locator like (id, name,
classname, linktext, partialLinkText, cssSelector, xpath).

Syntax: driver.FindElement(By.XPath(" ");


driver.FindElement(By.XPath("//span[text()='Downloads']"));

2. FindElements: This method returns a list of all elements that matches with the specified locator.

Syntax: driver.FindElements(By.Id(""));
Ex: driver.FindElements(By.Id("//a"));

WebDriver Methods:
1. Manage: we can set the timeouts for the webdriver using the Manage().TimeOuts() method.

This includes implicit wait and page load timeout.

We can manage the cookies using the Manage().cookies.

We can navigate forward and backward in the browser history using Manage.Navigation.

2. switchTo: This method allows us to switch to a different frame or window.

Ex: driver.SwitchTo().Alert().Accept();

Ex:

[TestClass]
public class Practise2
{
private ReadOnlyCollection<string> handles;

[TestMethod]
public void test()
{
IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait =TimeSpan.FromSeconds(5);
driver.Url = "https://secure.indeed.com/auth?hl=en_IN&co=IN&continue=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fin.indeed.com%2F%3Fr
%3Dus&tmpl=desktop&service=my&from=gnav-util-homepage&jsContinue=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fin.indeed.com
%2F&empContinue=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Faccount.indeed.com%2Fmyaccess&_ga=2.11004604.942636905.1682611851-
567037357.1682611851";
driver.FindElement(By.XPath("//button[@id='login-google-button']")).Click();
driver.FindElement(By.XPath("//button[@id='login-facebook-button']")).Click();
handles =driver.WindowHandles;

foreach(String handle in handles)


{
driver.SwitchTo().Window(handle);
string title = driver.Title;
Console.WriteLine(title);
}
}

}
3. Quit: This method closes the current window and ends the WebDriver session.

Syntax: driver.Quit();

4. PageSource: PageSource property is used to get the HTML source code of a web page.

Syntax: String Page = driver.PageSource;


5. CurrentWindowHandle: CurrentWindowHandle is a property in the selenium WebDriver interface thatis used to get the unique
identifier of the currently focused browser window or tab.

Syntax: String currentUrl = driver.CurrentWindowHandle;

6. WindowHandles: WindowHandles is a property in the selenium WebDriver interface that is used to get a set of all the unique
identifiers of the open browser windows or tabs.

Syntax: ReadOnlyCollection<String> handles = driver.WindowHandles;

7. Close: Close is a method in the Selenium WebDriver interface that is used to close the current browser.

Syntax: driver.Close();

8. Navigate: Navigate is a method in the Selenium WebDriver interface that is used to a URL or perform browser navigation
actions such as navigating forward or backward in the browser.

Syntax: driver.Navigate().Forward();
driver.Navigate().Back();
driver.Navigate().Refresh();

9. Title: Title is a property in the selenium WebDriver interface that is used to get the title of the current browser.

Syntax: String title =driver.Title;

WebElement methods:

1. TagName: TagName is a property in the selenium WebDriver interface that is used to get the HTML tag name of a web page
element.

Syntax: driver.FindElement(By.TagName(""));

2. Text: Text is a property in the Selenium that is used to get the text content of a web page element. We can use this property to
retrieve the text content of an element and perform further operations based on it.

Syntax: IWebElement element = driver.FindElement(By.CssSelector("p"));


String text = element.Text;

3.Enabled: Enabled is a property in the selenium that is used to check if a webpage element is enabled or not.In c#, we can use this
property to check if an element is enabled and perform further operations based on it.

Syntax: bool isEnabled = element.Enabled;

3. Selected: Selected is property in the selenium that is used to check if a web page element is selected or not.

Syntax: bool isSelected =element.Selected;

4. Location: Location is a property in the selenium WebElement interface that is used to get the location of a web page element.

Syntax: Point loc = element.Location;

5. Size: Size is a property in the selenium that is used to get the size of a web page element.

Syntax: Size size = element.Size;

6. Displayed: Displayed is a property in the selenium that is used to check if a web page element is currently displayed on the
page.

Syntax: bool isDisplayed = element.Displayed;

7. Clear: Clear is a method in selenium that is used to clear the text from the web page element that accepts input(ex: an input
field).
Syntax: input.Clear();

8. SendKeys: SendKeys is a method in selenium that is used to simulate typing Keys into a web page element that accepts input.

Syntax: input.SendKeys();

9. Submit: Submit is a method in selenium that is used to simulate typing keys into a web page element that accepts input.

Syntax: input.SendKeys();

10. Click: Click is a method in selenium that is used to click an element on a web page.

Syntax: button.Click();

11. GetAttribute: GetAttribute is a method in the selenium that is used to retrieve the value of a specified attribute of a web
element.

Syntax: String attribute =element.GetAttribute(“src”);

12. GetProperty: GetProperty is method is selenium that is used to retrieve the valus of a specified CSS property of a web element.

Syntax: String colorValue = paragrapgh.GetProperty(“color”);

13. GetCssValue: GetCssValue is a method in the selenium that is used to retrieve the value of a specified CSS property of a web
element.

Syntax: IWebElement paragraph = driver.FindElement(By.CssSelector(“#main-content > p”));

String colorValue =paragraph.GetCssValue(“color”);

You might also like