Selenium-Search-Context - Notes By ShariqSP

Practice Section

Element with ID

Link Text
Partial Link Text Example

Element with Class Name

Tag Name Example

Element with CSS Selector

Search Context and Locators

WebElement FindElement(By byObj)

This method is used to locate a single web element.

List FindElements(By byObj)

This method is used to locate multiple elements.

Note:

The above methods are implemented in RemoteWebDriver and inherited by each of the web drivers.

WebElement Interface

The WebElement interface represents an HTML element in Selenium.

It extends the SearchContext and TakesScreenshot interfaces.

By Class

The By class is a mechanism used to locate elements within a document in Selenium.

It provides static methods for creating locator objects based on different strategies.

Locator Methods in Selenium

id(): This method is used to locate elements by their ID attribute. It is one of the fastest and most reliable ways to locate elements, as IDs should be unique within a webpage.

name(): The name() method is used to locate elements by their name attribute. Unlike IDs, names do not have to be unique, so this method is typically used when dealing with elements that share the same name.

linkText(): This method is used to locate anchor elements () by their exact text content. It is useful for finding links that have a specific visible text.

partialLinkText(): Similar to linkText(), this method is used to locate anchor elements by a portion of their text content. It's helpful when the complete link text is too long or when you only need to match part of it.

className(): The className() method is used to locate elements by their class attribute. It's commonly used when elements have a specific class name assigned to them. Be cautious when using this method as classes can be shared among multiple elements.

tagName(): This method is used to locate elements by their tag name. It selects all elements that match the specified HTML tag. It's useful when you want to find multiple elements of the same type.

cssSelector(): CSS selectors are powerful patterns used to select HTML elements. The cssSelector() method allows you to locate elements using CSS selector syntax, providing flexibility and precision in targeting elements based on various attributes.

xPath(): XPath is a language for navigating through XML documents, but it's commonly used in web scraping and Selenium for locating elements. The xPath() method allows you to locate elements using XPath expressions, which can traverse the structure of the HTML document, selecting elements based on their relationships, attributes, or other properties.

sendKeys(charSequence...ch)

This method is used to enter data into a text field.

click()

This method is used to click on an element.

Examples of Locator Methods:

                        

By.id()

System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("https://demowebshop.tricentis.com/"); driver.findElement(By.id("small-searchterms")).sendKeys("Shariq");
                        

By.name()

System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("https://demowebshop.tricentis.com/register"); driver.findElement(By.name("q")).sendKeys("hello"); driver.findElement(By.name("Gender")).click();
                        

By.className()

System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("https://demowebshop.tricentis.com/"); driver.findElement(By.className("cart-label")).click();
                        

By.linkText()

System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("https://demowebshop.tricentis.com/"); driver.findElement(By.linkText("Register")).click();
                        

By.partialLinkText()

System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("https://demowebshop.tricentis.com/"); driver.findElement(By.partialLinkText("Books")).click(); driver.close();
                        

By.cssSelector()

System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("https://demowebshop.tricentis.com/"); driver.findElement(By.cssSelector("a[href='/register']")).click(); driver.close();
                        

By.xpath()

System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("https://demowebshop.tricentis.com/"); driver.findElement(By.xpath("//input[@value='Search']")).click(); driver.close();

Using contains() in XPath:

The contains() function searches for elements containing a specific text within an XPath expression.

Independent and Dependent Concept in XPath Expression:

When the text value of the elements is completely changing, then we can’t use functions like "contains()". In such cases, we identify the dynamically changing element using the nearby unique element. We call this concept as independent dependent XPath.

Steps to derive XPath expression using Independent dependent concept:

  1. Identify the nearby element which is stable and can be located easily. In this case, near to Input 2 we have Input 1. This input 1 is now the independent element and Input 2 is the dependent element.
  2. Identify the immediate common parent of the independent and dependent element. In this case, the first <tr> is the common parent.
  3. Derive the relative xpath to this common parent considering independent Input 1 as the relative reference.

Example:

        xpath=//input[@id='testId']/../../td[2]/input  —---> Locates Input 2
                    

Variable Arguments (Varargs) in Java:

Variable Arguments (Varargs) in Java is a method that takes a variable number of arguments.

Internally, the Varargs method is implemented by using the single dimensions arrays concept. Hence, in the Varargs method, we can differentiate arguments by using Index. A variable-length argument is specified by three periods or dots(…).

Example:

        public static void main(String[] args) {
            add(1,2,3,4,5,6,7,8,9,10);
        }
        public static void add(int...a)
        {
            for(int i : a)
                System.out.println(i);
        }
                    

Search Context Interview Questions

  1. What is a Search Context in WebDriver?
  2. Explain the concept of hierarchical searching in WebDriver.
  3. What are the different types of search contexts in WebDriver?
  4. How does WebDriver locate elements within a Search Context?
  5. What is the default Search Context in WebDriver?
  6. How do you switch between different Search Contexts in WebDriver?
  7. Explain the difference between findElement() and findElements() in terms of Search Context.
  8. How do you locate elements relative to another element in WebDriver?
  9. What is the use of the By class in WebDriver?
  10. Explain the significance of the parent-child relationship in Search Context.
  11. How does WebDriver handle nested elements within a Search Context?
  12. What are the limitations of using XPath for locating elements in WebDriver?
  13. How do you handle dynamic IDs and attributes while locating elements?
  14. Explain the concept of CSS Selectors in WebDriver.
  15. What is the difference between locating elements by ID and by CSS Selector?
  16. How do you handle locating elements by class name in WebDriver?
  17. Explain the importance of robust locators in WebDriver automation.
  18. What strategies do you employ to improve element locating performance in WebDriver?
  19. How do you handle locating elements within iframes using WebDriver?
  20. Explain how you would handle dynamic content on a webpage using WebDriver.

Multiple Choice Questions (MCQs)

  1. Which method is used to find a single element within a Search Context in WebDriver?
    1. findElement()
    2. locateElement()
    3. searchElement()
    4. find()
  2. What is the default Search Context in WebDriver?
    1. Current window
    2. Parent window
    3. Child window
    4. Root element
  3. Which of the following locators is the most preferred for locating elements in WebDriver?
    1. Partial link text
    2. Tag name
    3. CSS Selector
    4. Link text
  4. How do you locate an element by its CSS class in WebDriver?
    1. By.className()
    2. By.cssSelector()
    3. By.id()
    4. By.tagName()
  5. What is the purpose of the SearchContext interface in WebDriver?
    1. To provide methods for locating elements within a webpage.
    2. To represent the browser window.
    3. To manage cookies.
    4. To handle alerts and pop-ups.
  6. How do you switch to an iframe in WebDriver?
    1. switchToFrame()
    2. switchTo().frame()
    3. switchTo().frame(index)
    4. switchTo().defaultContent()
  7. Which method is used to find multiple elements within a Search Context in WebDriver?
    1. findElements()
    2. locateElements()
    3. searchElements()
    4. findAll()
  8. What is the significance of implicit wait in WebDriver?
    1. To wait for a certain condition to occur before executing the next command.
    2. To specify the time WebDriver should wait when locating elements.
    3. To handle asynchronous execution of JavaScript.
    4. To wait for the page to load completely.
  9. Which method is used to locate an element by its XPath in WebDriver?
    1. By.xpath()
    2. By.cssSelector()
    3. By.id()
    4. By.tagName()
  10. What is the purpose of the WebElement interface in WebDriver?
    1. To represent a single element within a webpage.
    2. To manage browser windows and tabs.
    3. To handle alerts and pop-ups.
    4. To interact with dropdown menus.