MainMenu

Home Core Java Overview Maven Tutorials

Saturday, 14 January 2017

Basic Selenium Accessing WebElements In Selenium

For Video : Click Here


Access Elements in Selenium : WebDriver driver = new FirefoxDriver();
By this above line of code a default firefox browser will be launched means we have Initiated a FirefoxDriver class with no parameter. obj is the name of object, you can give any name of object as per your convenience.
----------------------------------------------------------------------------------------------------------

driver.get("URL")
driver.navigate().to("URL")
get() method will launch a new browser session and directs to the given URL.
Note : for single page application, navigate().to() will not refresh the page but get() will do also with navigate().back() we can navigate to the previous page.
-----------------------------------------------------------------------------------------------------------

driver.close()
close() method is used to close the browser or page currently which is having the focus..
----------------------------------------------------------------------------------------------------------

driver.dispose()
dispose() method is used to close all browser windows and safely end the sessions.
----------------------------------------------------------------------------------------------------------

driver.quit()
quit() method is used to shut down the web driver instance and close the all the browsers opened by selenium.
----------------------------------------------------------------------------------------------------------

findElement(By.locator())
This method is used to find the Elements in GUI.
There are different kinds of locator() :
By.className : this will find the value based on class Name
By.cssSelector : this will find the value based on cssSelector
By.id : this will find the value based on id attribute
By.linkText : this will find the link element by the exact text
By.partialLinkText : this will locate elements by the given link text
By.name : this will find the value based on name attribute
By.tagName : this will find the value based on tagname
By.xpath : It will locate elements via xpath
Note : xpath is an addon on firefox through which xpath can be received.
---------------------------------------------------------------------------------------------------------
WebElement.senKeys("text") : This is used to send data or text inside a box.
WebElement.clear() : This is used to clear data in input box.
WebElement.click() : This is used to click on link.
---------------------------------------------------------------------------------------------------------

How to create xpath?

xpath can be created manually also :
syntax : //tagname[@attributname='value of attribute']
for Text : //td[text()='name of text']
or for input Text box : //input[text()='name of text']
or //input[@id = "text"]
for link : //a[@href = 'http://www.way2testing.com']

xpath contains : //*[contains(@id, 'value')]
xpath following :Xpath = //*[@type='text']//following::input
xpath starts-with : .//*[starts-with(@id, 'value')]
-------------------------------------------------------------------------------------------------

Code to press ENTER in selenium


WebDriver driver = new FirefoxDriver();
Actions act = new Actions(driver);
act.sendKeys(Keys.ENTER).build().perform();
----------------------------------------------------------------------------------------------

Different Kind of waits in selenium


In Selenium There is different kind of Waits :
1). Thread.sleep(2000L); //wait for 2 seconds
2.)Page Load : It will set the amount of time to wait for a page load to complete before throwing an error.
obj.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
3). Implicit Wait : Implicit wait timeout once set, is set for the lifetime of the Webdriver Object Instance.
obj.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
This will wait upto 10 seconds, if elements is find before 10 second then it will move to next step.
4).Explicit Wait : 1.Explicit wait will make your code to wait for vertain condition to occur before moving forward.
2.This can be acieved with combination of WebDriverWait and ExpectedConditions.
3.WebDriverWait by default calls ExpectedCondition to poll by every 500 milliseconds until it return successfully.
WebDriverWait objwait = new WebDriverWait(obj, 10); // this will wait for 10 seconds
objwait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(""));

Example of Explicit Wait(Commond)


1). First Locate WebElement.
2). Create WebDriverWait object and pass reference of WebDriver & Time.
3). Implement conditions.

WebElement obj = driver.findElement(By.cssSelector(".innerWrap>:nth-child(1)>:nth-child(2)"));
WebDriverWait objwait = new WebDriverWait(driver, 10);
objwait.until(ExpectedConditions.visibilityOf(obj));
----------------------------------------------------------------------------------------------

Code to Select Dropbox

Import the package "Select"
Select obj_name = new Select(driver.findElement(By.locator()));
obj_name.selectByVisibleText("text"); OR obj_name.selectByValue(); OR obj_name.selectByIndex();
For deselect : obj_name.deselectAll();
-----------------------------------------------------------------------------------------------

Code for Mouse hover in selenium

Import the Actions and Action class
WebDriver driver = new FirefoxDriver();
Actions act = new Actions(driver);
WebElement element = driver.findElement(By.locatior());
Action obj4 = obj2.moveToElement(element).build();
obj4.perform();
-----------------------------------------------------------------------------------------------

Code to perform Drag and Drop in selenium

Import the Actions class
WebDriver driver = new FirefoxDriver();
Actions action = new Actions(driver);
WebElement drag = driver.findElement(By.locatior());
WebElement drop = driver.findElement(By.locatior());
action.dragAndDrop(drag, drop).build().perform();
-----------------------------------------------------------------------------------------------
Important Points in Selenium :

How to press enter key in selenium?


driver.findElement(By.id("Value")).sendKeys(Keys.ENTER);

How to press enter key "ctrl" & "t" or "tab" in selenium?


Robot robot = new Robot();
robot.keyPress(keyEvent.VK_CONTROL);
robot.keyPress(keyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);

How to press enter key "ctrl" & "a" in selenium?

driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "a"));

How to scroll in selenium?


Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_PAGE_DOWN);
robot.keyRelease(KeyEvent.VK_PAGE_DOWN);

There is also scroll method(horizontal, vertical) i.e. scroll(10, 200)
((JavascriptExecutor)drive).executeScript("scroll(10,300)");

((JavascriptExecutor)drive).executeScript("window.scrollBy(10,300)");

((JavascriptExecutor)drive).executeScript("window.scrollTo(0, document.body.scrollHeight)");

Scroll up to an element WebElement element = driver.findElement(By.xpath(""));
((JavascriptExecutor)drive).executeScript("arguments[0].scrollIntoView();" element); How to handel alert & popup
Alert alert = driver.scitchTo().alert();
alert.accept();

driver.switchTo().ActiveElement();

driver.getWindowHandler();

Tags :-

How to open browser in selenium?

,

How to press Enter Key in selenium?

,

Different kinds of Waits in selenium

,

Explicit wait in selenium with example

,

How to select the value from Dropbox

,

How to perform mouse hover in selenium

,

how to press control + t in selenium

How to scroll in selenium

,

How to perform drag and drop action in selenium.

.

No comments:

Post a Comment