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

Creating A Selenium Script

This document discusses steps to create a Selenium script. It will launch Google Chrome, search for "Fresco Play", navigate to the Fresco Apps result, print the title, and quit the browser. It also covers locating elements using findElement() and findElements(), and using XPath locators with examples of absolute and relative XPath syntax.

Uploaded by

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

Creating A Selenium Script

This document discusses steps to create a Selenium script. It will launch Google Chrome, search for "Fresco Play", navigate to the Fresco Apps result, print the title, and quit the browser. It also covers locating elements using findElement() and findElements(), and using XPath locators with examples of absolute and relative XPath syntax.

Uploaded by

swati sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating a Selenium Script

In this scripting, we will do the following steps.


 Launch a google and try to search for "Fresco Play".
 Once we get the google results, we will be navigating to "Fresco Apps".

Creating a Selenium Script


Launch Browser
We will use the following code.

// Launching Google from Firefox with the help of "gecko driver."

System.setProperty("webdriver.gecko.driver", "Need to get Gecko driver


Path" );

WebDriver driver = new FirefoxDriver();

driver.get("https://google.com");

// We are getting the page title and printing it in a console to verify


that we are in the right page.

System.out.println("Page Title : " + driver.getTitle());

Creating a Selenium Script


 Once you have successfully launched "www.google.com", search for "Fresco
Apps" on Google.
 Once you get the search results, navigate to "Fresco Apps" as shown in the
sample code below.

Creating a Selenium Script


Once you navigate to the "Fresco Apps" page, print the page title in a console and
will quit the browser.

//Searching for "Fresco Play" in Google search

driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys("Fresco Play");

This study source was downloaded by 100000815997471 from CourseHero.com on 12-19-2021 09:15:51 GMT -06:00

https://www.coursehero.com/file/95282284/Creating-a-Selenium-Scriptdocx/
driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys(Keys.ENTER);

Thread.sleep(5000);
System.out.println("Page Title : " + driver.getTitle());

//Click "Fresco Apps" link from the search list

driver.findElement(By.xpath("//*[@id='rso']/div/div/div[2]/div/div/h3/a")).
click();

Thread.sleep(5000);
System.out.println("Page Title : " + driver.getTitle());

driver.quit();

Locate Elements
Most important step in test automation is to identify the web page elements
accurately. Selenium Webdriver performs it with help of
***findElement()***&***findElements()***.

 findElement(): This method helps to access a single web element on a


page . If it doesn't find the element, it will throw an exception.

 findElements(): This method returns the list of all matching elements. If it


doesn't find the elements, it will return an empty list.

Types of XPath
Element Locators help to find the element on the web pages accurately. Let’s start
with Syntax XPath. It can be written in two types as below.
Xpath=//tagname[@attribute='value']

 //: Select current node.


 Tagname: Tagname of the particular node.(Input)
 @: Select attribute.
 Attribute: Attribute name of the node. ( id, value, name, type, src, and
alt )
 Value: Value of the attribute. (login_id, text_value, username,
login_button,/images/btn_signin.gif and SignUp_button.)

This study source was downloaded by 100000815997471 from CourseHero.com on 12-19-2021 09:15:51 GMT -06:00

https://www.coursehero.com/file/95282284/Creating-a-Selenium-Scriptdocx/
Absolute XPath - It will be in long, and you can see HTML DOM Structure which
will be followed by single (/).
/html/body/div/selection/div[2]/div/div/input[1]
**Relative XPath– It will be short, and it will start in the middle of the HTML
DOM Structure which will be followed by double (//).
//input[@value='Google Search']

This study source was downloaded by 100000815997471 from CourseHero.com on 12-19-2021 09:15:51 GMT -06:00

https://www.coursehero.com/file/95282284/Creating-a-Selenium-Scriptdocx/
Powered by TCPDF (www.tcpdf.org)

You might also like