Handle ajax element using Selenium WebDriver
---------------------------------
package advancedWebdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class AjaxControl {
public WebDriver driver;
@
Test
public void googleAjax() throws Exception {
driver.get("http://google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("selenium");
Thread.sleep(1000);
String str = null;
str = driver.findElement(By.xpath("//*[@id='gsr']/table/tbody/tr/td[2]")).getText();
System.out.println(str);
String s[] = str.split("\n");
System.out.println(s.length);
for (int i = 0; i < s.length; i++) {
//System.out.println(s[i]);
if (s[i].equalsIgnoreCase("selenium tutorial"))
{
System.out.println(s[i]);
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys(s[i]);
}
}
}
@
BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver", "D:\\Library\\chromedriver.exe");
driver = new ChromeDriver();
}
@
AfterTest
public void afterTest() {}
}
Thanks, This was very helpful :)
ReplyDelete