Skip to content

Instantly share code, notes, and snippets.

@RDayal11
Created December 24, 2021 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RDayal11/215cf7550b55981c59445ab253ff807c to your computer and use it in GitHub Desktop.
Save RDayal11/215cf7550b55981c59445ab253ff807c to your computer and use it in GitHub Desktop.
package LambdaTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
@Listeners({util.Listener.class})
class AutomationUsingFindElementByText {
public String username = "YOUR USERNAME";
public String accesskey = "YOUR ACCESSKEY";
public static RemoteWebDriver driver = null;
public String gridURL = "@hub.lambdatest.com/wd/hub";
@BeforeTest
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("version", "96.0");
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
capabilities.setCapability("build", "AutomationUsingFindElement");
capabilities.setCapability("name", "AutomationUsingFindElementSuite");
try {
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Test
public void findElementByCompleteTextMatch() {
try {
System.out.println("Logging into Lambda Test Selenium Playground");
driver.get("http://labs.lambdatest.com/selenium-playground/");
WebElement checkBoxDemoPage= driver.findElement(By.xpath("//a[text()='Checkbox Demo']"));
checkBoxDemoPage.click();
System.out.println("Clicked on the Checkbox Demo Page");
WebElement header=driver.findElement(By.xpath("//h1"));
System.out.println("The header of the page is:"+header.getText());
} catch (Exception e) {
}
}
@Test
public void findElementByPartialTextMatch() {
try {
System.out.println("Logging into Lambda Test Selenium Playground");
driver.get("http://labs.lambdatest.com/selenium-playground/");
List<WebElement> tableOptions= driver.findElements(By.xpath("//a[contains(text(),'Table')]"));
for(WebElement e: tableOptions){
System.out.println("The different options with table in name are:"+e.getText());
}
} catch (Exception e) {
}
}
@AfterTest
public void closeBrowser() {
driver.close();
System.out.println("The driver has been closed.");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>LambdaTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.3</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="AutomationUsingFindElementSuite">
<test name="AutomationUsingFindElementTest" >
<classes>
<class name="LambdaTest.AutomationUsingFindElementByText" >
</class>
</classes>
</test>
</suite>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment