How to execute multiple key actions using webdriver with java - java

This is my code that opens a webpage and feeds input text into textfield and tries to browse further automatically. The first code: element.sendKeys(Keys.ENTER); is working but the next two lines immediately after this have no effect in my program. I am stuck here. Pls help me
public class GoogleSuggest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("vtu results");
element.sendKeys(Keys.ENTER);
element.sendKeys(Keys.TAB);
element.sendKeys(Keys.Enter);
System.out.println("Page title is: " + driver.getTitle());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("v");
}
});
System.out.println("Page title is: " + driver.getTitle());
}
}

Not clear what all actions you want perform but following working for me.
Try this out:
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
Actions action = new Actions(driver);
element.sendKeys("result");
action.sendKeys(element,Keys.ENTER).perform();
action.sendKeys(Keys.TAB);
Thread.sleep(2000);
action.sendKeys(Keys.ENTER).perform();
Thread.sleep(2000);
System.out.println("Page title is: " + driver.getTitle());
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("R");
}
});
System.out.println("Page title is: " + driver.getTitle());
}

Related

"driver cannot be resolved" in Java program

I'm new to the programming world so I wouldn't know how to fix this.
`#Test
public void LoginEmail() {
driver.findElement(By.id("email_button")).sendKeys("xxxxxxxx#gmail.com");`
At driver.findElement , driver is underlined red. When I hover over it these are my options.
Couldn't copy the options, so I took a screenshot:
you should initialize it first :
try {
WebDriver driver = new AndroidDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
} catch (Exception e) {
e.printStackTrace();
}
AndroidDriver driver = new AndroidDriver(new URL("localhost:4723/wd/hub"), cap);
This should be a class or local accessibility. Declare as a class object i.e in this text fixture or in the parent class which is inherited by this class or initialize locally i.e. in the test method.

How do I fill the email section using chrome driver

enter image description here
Can you help me to input text on the email box? The email box appears when I click on masuk button on the top, but I can't get to sendkeys on the email box.
H|ere is the url- www.tokopedia.com
And here is the code that does not work
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.tokopedia.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("login-ddl-link"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.findElement(By.id("login-ddl-link")).click();
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes213");
WebElement myPassword = driver.findElement(By.id("inputPassword"));
myPassword.sendKeys("tes123");
}
This is because authorization form located inside iframe element. You need to switch to that frame at first and then handle input fields:
...
driver.findElement(By.id("login-ddl-link")).click();
Thread.sleep(2000);
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes123");
...
To switch back you might need to use
driver.switchTo().defaultContent();
P.S. You don't need to click on input field to sent text to it, so driver.findElement(By.id("inputEmail")).click(); is redundant line
use this code it worked just fine for me on Chrome.
public static void main(String [] ar) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.tokopedia.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.xpath("//*[#id='login-ddl-link']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes213");
WebElement myPassword = driver.findElement(By.id("inputPassword"));
myPassword.sendKeys("tes123");
driver.findElement(By.xpath(".//*[#id='global_login_btn']")).click();
}

Selenium unable to find element in collapsable div

I was trying to automate the Make My Trip site using Selenium. These are the steps I took:
Search for MakeMyTrip in Google -> Done
Open makemytrip and change country to US -> Done
Click on feedback -> Done
Trying to fill feedback form -> Error
It's saying, unable to find the element.
I have tried the following:
1. Tried finding the element by id
2. Tried finding the element by xpath
//div[#class='feedback-form-container']//form[#id='feedbackForm']//input[#id='field_name_NAME']"
Code:
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseURL = "http://www.google.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void makeMyTriptest() throws Exception {
System.out.println("Entered this loop");
driver.get(baseURL + "/?gws_rd=ssl");
driver.findElement(By.id("lst-ib")).sendKeys("makemytrip");
System.out.println("send keys successful");
driver.findElement(By.linkText("Flights - MakeMyTrip")).click();
driver.findElement(By.id("country_links")).click();
driver.findElement(By.xpath("//*[#id='country_dropdown']//p//a[#href='http://us.makemytrip.com/']")).click();
driver.findElement(By.xpath("//div[#id='webklipper-publisher-widget-container-content-expand-collapse']")).click();
//entering feedback details
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//driver.findElement(By.id("field_name_NAME")).sendKeys("SubbaRao");
driver.findElement(By.xpath("//div[#class='feedback-form-container']//form[#id='feedbackForm']//input[#id='field_name_NAME']")).sendKeys("SubbaRao");
//driver.findElement(By.id("field_email_EMAIL")).sendKeys("test#test.com");
}
The Feedback form is located inside an iframe. You have to switch into it's context:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//WebElement iFrame = driver.findElement(By.xpath("//*[id='print_ticket_overlayiframe']"));
driver.switchTo().defaultContent();
driver.switchTo().frame("webklipper-publisher-widget-container-frame");
//driver.findElement(By.id("field_name_NAME")).sendKeys("SubbaRao");
driver.findElement(By.xpath("//*[#id='field_name_NAME']")).sendKeys("SubbaRao");
now while you are in the iframe, search for the input
Works for me.

Unable to identify the login button for naukri.com application using selenium Webdriver

I am trying to automate the naukri.com application and as a part of that when I launch the site it basically displays some set of pop up windows which needs to be closed before I proceed to login to the application.This specific functionality has been handled by the code where it closes all the pop up windows and when I proceed to click on the login button, the login button link is not identified and script fails.If i comment the pop up window code then the login button is identified.Please find the below code for the same and kindly help me to resolve the issue
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pagelaunch() throws InterruptedException{
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
Iterator<String> it =pops.iterator();
while (it.hasNext()){
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
System.out.println("the system handle is"+parenthandle);
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#title='Jobseeker Login']")).click();
Thread.sleep(5000);
for(String winhandle:driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
}
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[4]/div[2]/input")).sendKeys("anand_qa");
driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[5]/div[2]/input")).sendKeys("test1234");
driver.findElement(By.xpath("//div[8]/div[2]/div[2]/form/div[7]/button")).click();
driver.switchTo().window(parenthandle);
}
}
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pageLaunch() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
{
Iterator<String> it =pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
}
System.out.println("the system handle is"+ driver.switchTo().window(parenthandle).getTitle());
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#id='login_Layer']")).click();
Thread.sleep(5000);
/*for (String winhandle:driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
}*/
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath("//form[#id ='lgnFrm']/div[4]/div[2]/input[#id='uLogin']")).sendKeys("anand_qa");
driver.findElement(By.xpath("//form[#id ='lgnFrm']/div[5]/div[2]/input[#id='pLogin']")).sendKeys("test1234");
driver.findElement(By.xpath("//form[#id='lgnFrm']/div[7]/button")).click();
}
}
#AK17:
Your code had 2 problems
1.You were not switching to parenthandle after closing the pop-up windows, I added the code
driver.switchTo().window(parenthandle);
2.Your locators for username,password and login button were not correct
Working code, try this:
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pagelaunch() throws InterruptedException{
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
Iterator<String> it =pops.iterator();
while (it.hasNext()){
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
System.out.println("the system handle is"+parenthandle);
driver.switchTo().window(parenthandle);
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement login = driver.findElement(By.xpath("//a[#title='Jobseeker Login']"));
wait.until(ExpectedConditions.elementToBeClickable(login));
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#title='Jobseeker Login']")).click();
Thread.sleep(3000);
for(String winhandle:driver.getWindowHandles())
{
System.out.println("login: "+winhandle);
driver.switchTo().window(winhandle);
}
Thread.sleep(3000);
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath(".//*[#id='uLogin']")).sendKeys("anand_qa");
driver.findElement(By.xpath(".//*[#id='pLogin']")).sendKeys("test1234");
driver.findElement(By.xpath(".//*[#id='lgnFrm']/div[7]/button")).click();
//driver.switchTo().window(parenthandle);
}
}

Wait Till Text Present In Text Field

In webdriver, how to ask to webdriver to wait until text is present in text field.
actually i have one kendo text field whose values comes from database which takes some time to load. Once it load i can proceed further.
please help on this
You can use WebDriverWait. From docs example:
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(...).getText().length() != 0;
}
});
You can use WebDriverWait. From docs example:
above ans using .getTex() this is not returning text from input field
use .getAttribute("value") instead of getText()
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(...).getAttribute("value").length() != 0;
}
});
tested
100% working
hope this will help
A one liner that works and uses lambda function.
wait.until((ExpectedCondition<Boolean>) driver -> driver.findElement(By.id("elementId")).getAttribute("value").length() != 0);
Using WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait) and ExpectedCondition (org.openqa.selenium.support.ui.ExpectedConditions) objects
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("element_id"), "The Text"));
You can use a simple method in which you need to pass driver object webelement in which text is going to come and the text which is goint to come.
public static void waitForTextToAppear(WebDriver newDriver, String textToAppear, WebElement element) {
WebDriverWait wait = new WebDriverWait(newDriver,30);
wait.until(ExpectedConditions.textToBePresentInElement(element, textToAppear));
}
This is my solution for sending text to input:
public void sendKeysToElement(WebDriver driver, WebElement webElement, String text) {
WebDriverWait wait = new WebDriverWait(driver, Configuration.standardWaitTime);
try {
wait.until(ExpectedConditions.and(
ExpectedConditions.not(ExpectedConditions.attributeToBeNotEmpty(webElement, "value")),
ExpectedConditions.elementToBeClickable(webElement)));
webElement.sendKeys(text);
wait.until(ExpectedConditions.textToBePresentInElementValue(webElement, text));
activeElementFocusChange(driver);
} catch (Exception e) {
Configuration.printStackTraceException(e);
}
}
WebElement nameInput = driver.findElement(By.id("name"));
sendKeysToElement(driver, nameInput, "some text");

Categories