Day 13: Selenium functions

Day 13: Selenium functions

Selenium WebElement

The WebElement class in Selenium represents an individual element on a web page. It provides methods to interact with and manipulate web elements. Here's an example:

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

public class WebElementExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();

????// Navigate to a webpage
????driver.get("https://www.example.com");

????// Find an input element by its ID
????WebElement inputElement = driver.findElement(By.id("username"));

????// Type text into the input field
????inputElement.sendKeys("John Doe");

????// Click on a button element
????WebElement buttonElement = driver.findElement(By.tagName("button"));
????buttonElement.click();

????// Close the browser
????driver.quit();
??}
}

In the above example, we first create an instance of the ChromeDriver and navigate to a webpage. Then, we use the findElement() method of the WebDriver interface along with a locator strategy (in this case, By.id()) to locate and store a reference to an input element and a button element. We interact with the elements by calling methods like sendKeys() and click() on them.

WebDriver:

WebDriver is the main interface of Selenium that provides methods to control browsers and interact with web elements. It acts as a bridge between your tests and the browser. Here's an example:

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

public class WebDriverExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();

????// Navigate to a webpage
????driver.get("https://www.example.com");

????// Get the current page title
????String title = driver.getTitle();
????System.out.println("Page title: " + title);

????// Get the current URL
????String currentUrl = driver.getCurrentUrl();
????System.out.println("Current URL: " + currentUrl);

????// Close the browser
????driver.quit();
??}
}

In this example, we create an instance of ChromeDriver, navigate to a webpage using the get() method, and then use the WebDriver methods getTitle() and getCurrentUrl() to retrieve the page title and current URL, respectively. Finally, we close the browser using the quit() method.

By:

The By class in Selenium provides various locator strategies to identify web elements on a page. It is used in conjunction with the findElement() or findElements() methods of WebDriver. Here's an example:

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

public class ByExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();

????// Navigate to a webpage
????driver.get("https://www.example.com");

????// Find an element by its ID
????WebElement elementById = driver.findElement(By.id("username"));

????// Find an element by its name
????WebElement elementByName = driver.findElement(By.name("email"));

????// Find an element by its class name
????WebElement elementByClassName = driver.findElement(By.className("button"));

????// Find an element by its CSS selector
????WebElement elementByCssSelector = driver.findElement(By.cssSelector("#password"));

????// Find an element by its XPath
????WebElement elementByXPath = driver.findElement(By.xpath("https://input[@type='submit']"));

????// Close the browser
????driver.quit();
??}
}

In this example, we use different locator strategies provided by the By class to locate web elements. We demonstrate locating elements by ID, name, class name, CSS selector, and XPath.

要查看或添加评论,请登录

Shree Krishna Priya J的更多文章

社区洞察

其他会员也浏览了