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> |
<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); |
// 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"); |
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); |
// 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"); |
driver.manage().deleteCookieNamed("cookie name");
Selectors
id
If you have the following html :
You can get it like this :
WebElement webElement = driver.findElement(By.id("mydivid")); |
WebElement webElement = driver.findElement(By.id("mydivid"));
class
If you have the following html :
<div class="myclass"></div>
<span class="myclass"></span> |
<div class="myclass"></div>
<span class="myclass"></span>
You can get it like this :
List<WebElement> webElement = driver.findElements(By.className("myclass")); |
List<WebElement> webElement = driver.findElements(By.className("myclass"));
tagName
If you have the following html :
<div>content1</div>
<div>content2</div> |
<div>content1</div>
<div>content2</div>
You can get it like this :
List<WebElement> webElement = driver.findElements(By.tagName("div")); |
List<WebElement> webElement = driver.findElements(By.tagName("div"));
name
If you have the following html :
<span name="myname"></span> |
<span name="myname"></span>
You can get it like this :
List<WebElement> webElement = driver.findElements(By.name("myname")); |
List<WebElement> webElement = driver.findElements(By.name("myname"));
Fill form
Input text
If you have the following html :
<input type="text"></input> |
<input type="text"></input>
You can fill it like this :
webElement.sendKeys("hello world"); |
webElement.sendKeys("hello world");