My linux world » Java – Selenium

Java - Selenium


Contents

Dependencies

If you use maven, add this to your pom.xml:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>${selenium.version}</version>
</dependency>

Cookies

Create cookie

// First create cookie (please have a look to selenium javadoc to have a list for all the constructors):
Cookie cookie = new Cookie("cookie name", "cookie value");
 
// now add cookie :
driver.manage().addCookie(cookie);

Read cookie

Cookie cookie = driver.manage().getCookieNamed("cookie name");

Update cookie

// First delete cookie :
driver.manage().deleteCookieNamed("cookie name");
 
// Then, add the new cookie :
Cookie cookie = new Cookie("cookie name", "cookie value");
driver.manage().addCookie(cookie);

Delete cookie

driver.manage().deleteCookieNamed("cookie name");

Selectors

id

If you have the following html :

<div id="mydivid"></div>

You can get it like this :

WebElement webElement = driver.findElement(By.id("mydivid"));

class

If you have the following html :

<div class="myclass"></div>
<span class="myclass"></span>

You can get it like this :

List<WebElement> webElement = driver.findElements(By.className("myclass"));

tagName

If you have the following html :

<div>content1</div>
<div>content2</div>

You can get it like this :

List<WebElement> webElement = driver.findElements(By.tagName("div"));

name

If you have the following html :

<span name="myname"></span>

You can get it like this :

List<WebElement> webElement = driver.findElements(By.name("myname"));

Fill form

Input text

If you have the following html :

<input type="text"></input>

You can fill it like this :

webElement.sendKeys("hello world");

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.