Alert handling in Selenium


public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
String Url = "http://sislands.com/coin70/week1/dialogbox.htm";
driver.get(Url);
WebElement alert = driver.findElement(By.xpath("///div[1]/center/table/tbody/tr/td/form[1]/p/input"));
alert.click();
Alert popup = driver.switchTo().alert();
System.out.println(popup.getText());
popup.accept();
Thread.sleep(3000);
driver.close();

}

Pick Date From a Calendar in Selenium



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

WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/datepicker/");
driver.manage().window().maximize();
driver.switchTo().frame(0);
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
driver.findElement(By.xpath("//*[@id='datepicker']")).click();
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div/a[2]/span")).click();
List column = driver.findElements(By.tagName("td"));
for (WebElement j : column)
{
if (j.getText().contains("13")){
j.findElement(By.linkText("13")).click();
break;
}
}
Thread.sleep(5000);
}

How to write dynamic Xpath to identify elements on Webpage?


We always have concerns about writing Dynamic Xpath. So, I've listed below some ways to write xpath with example. If you face any problem, Contact Us:

1) Based on location
2) Using Single Attributes
3) Using Multiple Attributes
4) Using functions


All examples are based on following HTML Code:

input class="search-field" name="s" placeholder="Search …" title="Search for:" type="search" value=""

Run your script in Internet Explorer (IE)


 public static void main(String[] args) throws InterruptedException { DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); System.setProperty("webdriver.ie.driver", "C:/IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(ieCapabilities); driver.get("http://google.com"); Thread.sleep(5000); driver.quit(); }

- Download IE Driver by Clicking Here