Software Testing Articles - Page 28 of 31

How to identify the elements by partially comparing to its attributes in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:57:32

1K+ Views

We can identify elements by partially comparing to its attributes in Selenium with the help of regular expression. In xpath, there is contains () method. It supports partial matching with the value of the attributes. This method comes as useful while dealing with elements having dynamic values in their attributes.Syntax driver.findElement(By.xpath("//tagname[contains(@attributes, ’value’)]"))In CSS, we can identify elements by partially comparing to its attributes by using regular expressions. There can be three scenarios −Using ^ to target attributes starting with a particular text.Syntax −driver.findElement(By.cssSelector("tagname[attribute^=’value’]"))Using $ to target attributes ending with a particular text.Syntax −driver.findElement(By.cssSelector("tagname[attribute$=’value’]"))Using * to target attributes containing a particular text.Syntax −driver.findElement(By.cssSelector("tagname[attribute*=’value’]"))Exampleimport org.openqa.selenium.By; ... Read More

How to identify elements based on text visible on page in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:59:04

3K+ Views

To identify elements based on text visible on page, text() method is used in xpath.Syntax −driver.findElement(By.xpath("//tagname[text()=’value’]"))Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class TextMatch {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://www.tutorialspoint.com/index.htm";       driver.get(url);       driver.manage().window().maximize();       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);       //xpath with text() method       driver.findElement(By.xpath("//*[text()=’GATE Exams’]")).click();       driver.close();    } }Read More

What are the various locators that Selenium supports?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:44:25

284 Views

The various types of locators that Selenium support are listed below −ID − This attribute is unique for every element.Syntax − driver.findElement(By.id("")).Name − This attribute is not unique for every element.Syntax − driver.findElement(By.name("")).CSS Selector − This can be derived from element tags and attributes.Syntax −driver.findElement(By.cssSelector("")).xpath − This can be derived from element tags and attributes.Syntax −driver.findElement(By.xpath("")).TagName − This can be derived from the HTML tags to identify the elements.Syntax − driver.findElement(By.tagName("")). LinkText − This can be derived from the anchor text to identify the elementszSyntax − driver.findElement(By.linkText("")).PartialLinkText − This can be derived from the partial anchor text to identify the elementsSyntax − driver.findElement(By.partialLinkText("")).Classname ... Read More

How to count the number of headers in a web table in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:39:18

1K+ Views

The total number of headers in the web table can be counted with the help of findElements() method. The logic is to return a list of web elements with xpath with the help of tag inside the table, then getting the size of that list.Code ImplementationExampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class TableHeaderCount {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver",       "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://www.tutorialspoint.com/plsql/plsql_basic_syntax.htm";       driver.get(url);     ... Read More

Mobile

How to reset or clear an edit box in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:30:20

1K+ Views

We can reset or clear an edit box in Selenium with the help of clear() method.Code Implementation with clear().Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ResetText {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver",    "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://www.tutorialspoint.com/index.htm";       driver.get(url);       driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);       //entering text in the edit box       driver.findElement(By.cssSelector("#gsc-i- id1")).sendKeys("Selenium");       Thread.sleep(1000);       // resetting text from the edit box with ... Read More

What are the various important exceptions in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:25:51

316 Views

The various important exceptions in Selenium are listed below −TimeOutException − This exception is thrown if an action does not complete in a particular duration. If the page element does not load even after the waits.driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS) ; driver.get(“https://www.tutorialspoint.com/index.htm” );In the above program, an implicit wait of 15 seconds is added. If the page https://www.tutorialspoint.com/index.htm doesn’t load in 15 seconds, then TimeoutException will be thrown.NoSuchElementException − This exception happens if the web element with particular attributes does not exist on the page. This exception class is a subclass of NotFoundException and is thrown if the driver is not successful in ... Read More

What are the different cookies methods in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:24:12

523 Views

The different cookies method in Selenium are listed below −driver.manage().deleteAllCookies() − This removes all cookies.driver.manage().deleteCookie(Id) − This removes a particular cookie.driver.manage().deleteCookieNamed(CookieName) − This removes a particular cookie based on Name.driver.manage().getCookies() − This returns all the cookies.driver.manage().getCookieNamed(CookieName) − This returns a particular cookie based on Name.driver.manage().addCookie(Id) − This adds a particular cookie.Code Implementation with some cookies method.Exampleimport org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class CookiesScripting {    public static void main(String[] args) {       // TODO Auto-generated method stub       System.setProperty("webdriver.chrome.driver",    "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       driver.manage().window().maximize();       // ... Read More

How to maximize the browser in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:22:43

4K+ Views

We can maximize the browser in Selenium with the help of method maximize().The present active window becomes maximized with this method.Code implementation.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class BrowserMax {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://www.tutorialspoint.com/index.htm";       driver.get(url);       // maximizing browser with maximize()       driver.manage().window().maximize();       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);       driver.findElement(By.partialLinkText("Coding")).click();       driver.close();    } }Read More

What is the difference between getWindowHandle() and getWindowHandles() in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:20:39

16K+ Views

getWindowHandle() and getWindowHandles() methods have some prominent differences.driver.getWindowHandles() – It stores the set of handles for all the pages opened simultaneously.driver.getWindowHandle() – It fetches the handle of the web page which is in focus. It gets the address of the active browser and it has a return type of String.Code ImplementationExampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.Set; import java.util.Iterator; import org.testng.annotations.Test; public class WindowHandles{    @Test    public void windowHandle() throws Exception {       System.setProperty("webdriver.chrome.driver", "C:\Selenium\chromedriver.exe");       WebDriver driver = new ChromeDriver();       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);     ... Read More

How to get the value of the edit box in Selenium?

Debomita Bhattacharjee
Updated on 10-Jun-2020 12:17:34

635 Views

We can get the value of the edit box in Selenium by the following ways −By using getText () method.Using the class JavascriptExecutor.Code Implementation with method getText ().Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class GetValueScripting {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://www.tutorialspoint.com/index.htm";       driver.get(url);       driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);       String text = driver.findElement(By.className("gsc-input")).getText();       System.out.println("Extracted text is " + text);       driver.close();   ... Read More

Advertisements