Text

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Locating with text:

package test;

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

public class Test {

public static void main(String[] args) throws


InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\
Users\\Laptop3\\eclipse-workspace\\Test1'\\driver\\
chromedriver.exe");

//Launch the Browser


WebDriver driver = new ChromeDriver();

//go to the URL


driver.get("https://en-gb.facebook.com/");

WebElement btnCreate =
driver.findElement(By.xpath("//a[text()='Create New
Account']"));
btnCreate.click();

Locating with partial text using contains

package test;

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

public class Test {


public static void main(String[] args) throws
InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\
Users\\Laptop3\\eclipse-workspace\\Test1'\\driver\\
chromedriver.exe");

//Launch the Browser


WebDriver driver = new ChromeDriver();

//go to the URL


driver.get("https://en-gb.facebook.com/");

WebElement btnCreate =
driver.findElement(By.xpath("//a[contains(text(),'New')]"));
btnCreate.click();

Locating with Partial Attribute using Contains

package test;

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

public class Test {

public static void main(String[] args) throws


InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\
Users\\Laptop3\\eclipse-workspace\\Test1'\\driver\\
chromedriver.exe");

//Launch the Browser


WebDriver driver = new ChromeDriver();
//go to the URL
driver.get("https://en-gb.facebook.com/");

WebElement btnCreate =
driver.findElement(By.xpath("//a[contains(@data-
testid,'registration')]"));
btnCreate.click();

GetText:

package test;

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

public class Test {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C:\\
Users\\Laptop3\\eclipse-workspace\\Test1'\\driver\\
chromedriver.exe");

//Launch the Browser


WebDriver driver = new ChromeDriver();

//go to the URL


driver.get("https://en-gb.facebook.com/");

WebElement txtFacebook =
driver.findElement(By.xpath("//h2[contains(text(),'helps')]"));
String text = txtFacebook.getText();
System.out.println(text);
}

GetAttributes(“values”)
package test;

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

public class Test {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C:\\
Users\\Laptop3\\eclipse-workspace\\Test1'\\driver\\
chromedriver.exe");

//Launch the Browser


WebDriver driver = new ChromeDriver();

//go to the URL


driver.get("https://en-gb.facebook.com/");

WebElement txtUserName =
driver.findElement(By.id("email"));
txtUserName.sendKeys("Test");
String att = txtUserName.getAttribute("value");
System.out.println(att);

You might also like