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

Pdfs Design30

The document explains how to use XPath with group indexing to fetch specific elements from a webpage, particularly focusing on fetching the 4th suggestion from Google search results. It also covers synchronization in Selenium, detailing implicit and explicit waits, and the use of Thread.sleep for managing timing issues in web automation. Key points include the syntax for XPath and wait methods, as well as the limitations of implicit waits.

Uploaded by

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

Pdfs Design30

The document explains how to use XPath with group indexing to fetch specific elements from a webpage, particularly focusing on fetching the 4th suggestion from Google search results. It also covers synchronization in Selenium, detailing implicit and explicit waits, and the use of Thread.sleep for managing timing issues in web automation. Key points include the syntax for XPath and wait methods, as well as the limitations of implicit waits.

Uploaded by

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

5.

X path by group Index:


When ever multiple elements are matching we use
group index to fetch the address of an element.

Syntax: (X path expression)[position value]


[1]
Script:open the browser and enter google url typr
your name and fetch the 4th value into console from
the suggestion
Note:
• index always start from 0.
• Position value always starts from 1.

Position value of 6 is 4.
Eg: (//input[@type=’radio’])[4]
Assignment: open browser enter google url type phone
and fetch the 4th value in console.
package Locators;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class X_Path_Group_Index {

public static void main(String[] args) throws


Throwable {
WebDriverManager.chromedriver().setup();
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
driver.get("https://www.google.com/");

driver.findElement(By.xpath("//textarea[@name='q'
]")).sendKeys("phone");
//Thread.sleep(5000);

List<WebElement> value =
driver.findElements(By.xpath("//li[@data-view-
type='1'][4]"));

for(WebElement b:value) {
System.out.println(b.getText());

Thread.sleep(5000);
driver.close();

Synchronization:
Synchronization: It is a process of matching the
selenium speed with Application speed.
Types of Synchronization:
1.Implicitly wait
2. Explicitly wait
3.Thread.sleep

1.Implicitly wait :
It is a selenium wait which is used to match the
selenium speed with application speed.
Syntax:
driver.manage().timeouts().implicitlyWait(Duration,
TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20,
TimeUnit.SECONDS);

driver ---->Reference variable


manage()---->WebDriver method
timeouts()---->method inside the options nested
interface.
implicitlyWait()---->method is used to set implicit wait
time
TimeUnit-----> it is an inbuilt class in java available in
util package
TimeUnit.SECONDS------>indicates that waiting period
is in seconds.

• The Implicit Wait in Selenium is used to tell the


web driver to wait for a certain amount of time
before it throws a “No Such Element Exception”.
• Once we set the time, the web driver will wait for
the element for that time before throwing an
• exception.
• Implicit wait is applicable for only findElement()
and findElements();
How to Write implicit wait in a code:
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
Explanation:
• It will search for the element in HTML tree
Structure.
• If Element is found it will returns the address
• If Element is not found it will check for the given
time.
• If the given time is not over it will internally wait
for 0.5 seconds which is called Polling Period
• If the given time is over, it will throw No such
element Exception
• This process repeats
1. Until the element is found
2. Until the given time is over
Drawbacks:
It is applicable only for findElement() and
findElements().

2.Explicitly Wait: It is a selenium wait which is


used to match the selenium speed with application
speed.
Syntax:
WebDriverWait wait=new
WebDriverWait(driver,timeoutinseconds);
Wait.until(Expected condition. visibilityOf(element)
elementToBeClickable(element)
titleContains(String));

WebDriverWait wait=new WebDriverWait(driver,10);


wait.until(ExpectedConditions.visibilityOf(ele));
wait.until(ExpectedConditions.elementToBeClickable(el
e).
Explanation:
It will search for the element in HTML tree Structure.
If Element is found it will returns the address
If Element is not found it willcheck for the given time.
If the given time is not over it will internally wait for
0.5 seconds which is called Polling Period
If the given time is over, it will throw TimeOut
Exception
This process repeats
1. Until the element is found
2. Until the given time is over
3.Thread.sleep
• It is java wait
• It is applicable for only particular line
• Syntax: Thread.Sleep()

You might also like