selenium element not interactable even after element is present - java

I have written a Java code for selenium and I'm trying to change the country on my website using selenium, somehow the code is running fine and the steps are executed as per the command line but when I check the execution of the test, the country isn't changed.
This is the code i have written
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class simpleCartFlow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","/Users/manavmehta/Desktop/squareoffSeleniumProjects/chromedriver");
WebDriver driver=new ChromeDriver();
driver.get("http://release.squareoffnow.com/");
driver.manage().window().setSize(new Dimension(1440, 789));
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// selectCountry.usaCountry(driver);
System.out.println("step 1 start");
driver.findElement(By.cssSelector(".selected-flag > .iti-flag")).click();
driver.manage().timeouts().implicitlyWait(60000, TimeUnit.MILLISECONDS);
driver.findElement(By.xpath("(//li[#id='iti-item-us'])")).click();
System.out.println("flag clicked pleaseeeeeeeee");
System.out.println("step 1 done");
driver.findElement(By.linkText("Products")).click();
// driver.quit();
driver.findElement(By.cssSelector(".store-buy-pro-button")).click();
driver.findElement(By.cssSelector(".pro-twinpack-button")).click();
driver.findElement(By.cssSelector(".whole-purchase-button")).click();
driver.findElement(By.cssSelector(".productAvailability > .click-button")).click();
driver.findElement(By.cssSelector(".giftpackSubmit")).click();
System.out.println("button not clicked");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.cssSelector(".checkout-anchor")).click();
System.out.println("button clicked");
details.addressDetails(driver);
// driver.findElement(By.id("name1")).click();
// driver.findElement(By.id("name1")).sendKeys("tester");
// driver.findElement(By.id("name2")).sendKeys("tester");
// driver.findElement(By.id("phone1")).sendKeys("13024025552");
// driver.findElement(By.id("email1")).sendKeys("XYZ#GMAIL.COM");
// driver.findElement(By.id("address1")).sendKeys("--");
//// driver.findElement(By.id("state1")).sendKeys("California");
// driver.findElement(By.id("zipcode1")).sendKeys("12085");
// driver.findElement(By.id("city1")).sendKeys("Guilderland Center");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.cssSelector(".secure-pic")).click();
driver.switchTo().frame(1);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("Field-numberInput")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("Field-numberInput")).sendKeys("4242 4242 4242 4242");
driver.findElement(By.id("Field-expiryInput")).sendKeys("04 / 22");
driver.findElement(By.id("Field-expiryInput")).sendKeys("04 / 44");
driver.findElement(By.id("Field-cvcInput")).sendKeys("323");
// driver.quit();
}
}
what I want to do is click on the flag, and change the country from the dropdown.
I did a check to find if the element is present, the value came as true. I also added implicit wait of 30 seconds!!
the issue is that the element is not interactable

Related

sendkeys() not working in selenium webdriver

Till yesterday the below mentioned code was working fine but now i am facing some problem .
The code opens firefox browser then loads facebook.com but the code is not sending
email and password to web browser i.e. sendkeys() is not working.
I verified the id of both textbox of email and password which are correct yet code is not working .
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
public class Webdriver2 {
WebDriver driver ;
JavascriptExecutor jse;
public void invokeBrowser()
{
try
{
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.19.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS );
driver.get("https://www.facebook.com/");
search();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void search()
{
try
{
driver.findElement(By.id("email")).sendKeys("example#gmail.com");
Thread.sleep(4000);
driver.findElement(By.id("pass")).sendKeys("password");
Thread.sleep(4000);
driver.findElement(By.id("u_0_2")).click();
Thread.sleep(4000);
/*driver.findElement(By.name("q")).sendKeys("spit mumbai");
Thread.sleep(4000);
driver.findElement(By.xpath(" //button[#aria-label='Search' and #data-testid='facebar_search_button'] ")).click();*/
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Webdriver2 w = new Webdriver2();
w.invokeBrowser();
}
}
Try clicking on the two textboxes before you are performing the sendKeys:
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).sendKeys("example#gmail.com");
The textboxes probably needs focus.
We need to take care of a couple of things here as follows:
The Email or Phone field is within an <input> tag so we need to take it into account while selecting the locator.
The Password field is also within an <input> tag so we need to take it into account while selecting the locator.
If you observe closely the id of the Log In button is dynamic and also within an <input> tag, so we need to consider this factor as well.
Here is the minimum sample code block using cssSelector to access the url https://www.facebook.com/, provide Email or Phone and Password, finally click on Log In button:
package demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Facebook_Login_CSS_FF {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.cssSelector("input#email")).sendKeys("Ryusei");
driver.findElement(By.cssSelector("input[name='pass']")).sendKeys("Nakamura");
driver.findElement(By.cssSelector("input[value='Log In']")).click();
}
}

(Should be easy) Selenium chromedriver (java) no element found error

I tried this exact code on other websites and it seems to work fine. It's just on pizza hut that it can't even locate an element let alone click on it. Thread.sleep() doesn't make a difference. The problem is between the commented *, according to the compiler. Here's the code.
package training;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PizzaHut {
WebDriver driver;
#Test
public void open() throws Exception {
//SET UP WEBDRIVER AND OPEN WEBSITE
System.setProperty("webdriver.chrome.driver","/Users/1mr4n/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.pizzahut.com/#/home");
Thread.sleep(1000);
//CLICK PIZZA
Thread.sleep(5000);
test("before");
//**************PROBLEM*CODE**********************
driver.findElement(By.xpath("//a[#id='lg-nav-pizza']")).click();
//************************************************
test("clicked pizza");
//CLOSE BROWSER
Thread.sleep(15000);
driver.close();
}
public static void test(String x) {
System.out.println(x);
}
}
I just ran the code below and it worked just fine. No need for sleep or waits and I'm using Chrome also.
driver.get("https://www.pizzahut.com");
driver.findElement(By.id("lg-nav-pizza")).click();
Try this one ,definitely it help you.
package training;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PizzaHut {
WebDriver driver;
#Test
public void open() throws Exception {
//SET UP WEBDRIVER AND OPEN WEBSITE
// System.setProperty("webdriver.chrome.driver","/Users/1mr4n/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.pizzahut.com/#/home");
Thread.sleep(1000);
//CLICK PIZZA
Thread.sleep(5000);
test("before");
List<WebElement> webElements=driver.findElement(By.cssSelector("div.btn-group.btn-group-lg.btn-group-justified.ph-header-navigation"))
.findElements(By.cssSelector("div.btn.btn-link.ph-ghost-padding.ng-scope"));
for (WebElement element : webElements) {
if (element.getAttribute("title").equals("PIZZA")) {
element.click();
}
}
Thread.sleep(5000);
test("clicked pizza");
//CLOSE BROWSER
Thread.sleep(15000);
driver.close();
}
public static void test(String x) {
System.out.println(x);
}
}
You are not able to click it because it is an AngularJS tag, hence it takes some time to load: add some wait like below and it works (Here i'm just adding thread.sleep for the sake of simplicity,I've increased it to 15000ms):
//CLICK PIZZA
Thread.sleep(15000);
test("before");
//**************PROBLEM*CODE**********************
driver.findElement(By.xpath("//a[#id='lg-nav-pizza']")).click();

Not able to perform drag & drop using Actions

package Chrome_Packg;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testFirefox_DragDrop {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
WebElement drag=driver.findElement(By.xpath("/html/body/div[1]"));//drag element
WebElement drop=driver.findElement(By.xpath("/html/body/div[2]"));//drop element
Actions action=new Actions(driver);
Thread.sleep(3000);
action.dragAndDrop(drag, drop).perform();
}
}
After executing the code, using Run as java application, in output I am getting nothing.
Here is the code snippet for the same website that you are trying on and you can also find the example here selenium Drag and Drop example
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://jqueryui.com/droppable/");
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
dragAndDrop(Sourcelocator,Destinationlocator);
String actualText=driver.findElement(By.cssSelector("#droppable>p")).getText();
Assert.assertEquals(actualText, "Dropped!");

Clicking Compose button in Gmail using Selenium WebDriver

The button pressing command doesn't work. It's finding the button, but isn't clicking the button. When clicking the button there should be a native page that opens within Gmail.
All the code below is attempting to click the button within the new contacts page of Gmail https://mail.google.com/mail/u/0/1#contact/new
Inspecting the element the div tag is div tabindex="0" aria-label="Email" data-tooltip="Email" aria-disabled="false" style="-moz-user-select: none;" id=":2l" class="T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3" role="button">div class="J-J5-Ji T-I-J3 Nz NS">/div>/div>
System.out.println("Finding Button");
driver.findElement(By.id(":2l")).click();
System.out.println("printing button");
System.out.println(driver.findElement(By.id(":2l")));
System.out.println("Finding button 2");
WebElement composeBtn = driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3']"));
System.out.println("Clicking button 2");
composeBtn.click();
System.out.println("Button 2 Clicked");
System.out.println(composeBtn.toString());
System.out.println("Finding button 3");
WebElement cBtn = driver.findElement(By.cssSelector("div[class= 'J-J5-Ji T-I-J3 Nz NS']"));
System.out.println("Clicking button 3");
cBtn.click();
Please let me know if you can help me identify this button
When I look at that page in the link you have provided, the compose button is grayed out and is not clickable. Having a program trying to click a button that a user could not click is still going to fail. Selenium will not and can not interact with objects that a user could not interact with (such as hidden fields, and in this case, grayed out buttons).
This method uses contains.
package testCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GmailFileUpload
{
WebDriver driver = null;
WebElement element = null;
#Before
public void setUp() throws Exception
{
File file = new File("G:\\Selenium\\All_Jars\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.manage().window().maximize();
}
#Test
public void test() throws InterruptedException, AWTException
{
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.id("Email")).sendKeys("aavinashpande#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();
driver.findElement(By.linkText("Gmail")).click();
Thread.sleep(5000);
//click on compose
//driver.findElement(By.xpath("//div[#class='T-I J-J5-Ji T-I-KE L3'] ")).click();
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//textarea[#name='to']")).sendKeys("aavinashpande#gmail.com");
driver.findElement(By.xpath("//input[#name='subjectbox']")).sendKeys("aavinashpande#gmail.com");
Thread.sleep(5000);
element = driver.findElement(By.xpath("//div[#class='Ar Au']//div"));
element.click();
element.sendKeys("Hi Avinash");
Thread.sleep(3000);
}
#After
public void teardown() throws Exception
{
driver.quit();
}
}
I find the send button like this:
driver.FindElement(By.XPath("//div[contains(text(),'Send')]")).Click();
After that you can do a quit. Just have an extra pop up ask you to confirm leaving the account:
driver.Navigate().GoToUrl("https://mail.google.com/mail/logout?hl=en");
I have sent an Emil successfully through selenium automation using Gmail Account with the below script.
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.google.co.in/";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#id=':jb']/div[#class='z0']/div")).click(); // Compose
selenium.type("//div[#class='wO nr l1']//textarea[#name='to']", "vikramn#gmail.com"); // For To
selenium.type("//div[#class='aoD az6']//input[#name='subjectbox']", "Wanted to SAY HI"); // For Subject
selenium.type("//div[#class='Ar Au']/div[#class='Am Al editable LW-avf']", "Hi Vikram");// For Message body
selenium.click("//div[#class='J-J5-Ji']/div[#class='T-I J-J5-Ji aoO T-I-atl L3']"); //send
You can use this code to compose email using selenium web driver for gmail
public void gmail() {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#aria-label='Email or phone']")).sendKeys("Your email");
driver.findElement(By.xpath("//span[.='Next']")).click();
//wait.until(ExpectedConditions.elementToBeClickable(password));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.xpath("//input[#aria-label='Enter your password']")).sendKeys("your password");
driver.findElement(By.xpath("//span[.='Next']")).click();
driver.findElement(By.xpath("//div[contains(text(),'Compose')]")).click();
}
driver.findElement(By.xpath("//*[#role='button' and text()='Compose']")).click();

How to handle same multiple windows e.g. google in Selenium WebDriver with Java

I have used the code below trying to open same multiple window "Google". Kindly help me in editing this and explaining how to handle this .
driver.switchTo().window("gbar");//not sure how to use this
and below code tried in Selenium:
package Testing;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import junit.framework.*;
public class Float {
public static void setUp() {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
}
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.name("q"));
element.click();
WebDriverWait wait = new WebDriverWait(driver, 80);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
element.sendKeys("hi");
element.clear();
Thread.sleep(2000);
element.sendKeys("hey");
element.submit();
setUp();
driver.switchTo().window("gbar");// //* not sure how to use this *///
WebElement element1 = driver.findElement(By.name("q"));
element1.click();
element1.sendKeys("hi");
element1.clear();
element1.sendKeys("hey");
element1.submit();
driver.quit();
}
}
You can get a handle to your window via driver.getWindowHandle()and you can switch to a window with driver.switchTo().window("handle");.
If you want to open a new window you can click on a link with target="_blank" on the website or execute JavaScript to open a new window. Then you'll find another handle in driver.getWindowHandles(). A possible way could be:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
List<String> knownHandles = new ArrayList<String>();
knownHandles.add(driver.getWindowHandle());
((JavascriptExecutor)driver).executeScript("window.open();");
// find the new handle. we are getting a set
for (String handle : driver.getWindowHandles()) {
if (!knownHandles.contains(handle)) {
knownHandles.add(handle);
break;
}
}
String newHandle = knownHandles.get(knownHandles.size() -1 );
driver.switchTo().window(newHandle);
driver.get("https://www.google.com");
Another way is to inject the anchor and click it via JavaScript.
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.manage().window().maximize();
//Close the new window, if that window no more required
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
Here is a sample example handling multiple windows:
public class Mytesting {
WebDriver driver = new FirefoxDriver();
#Before
public void beforetest() {
driver.manage().window().maximize();
driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
}
#Test
public void test () throws InterruptedException
{
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();
// Get and store both window handles in array
Set<String> AllWindowHandles = driver.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
String window2 = (String) AllWindowHandles.toArray()[1];
System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);
//Switch to window2(child window) and performing actions on it.
driver.switchTo().window(window2);
driver.findElement(By.xpath("//input[#name='fname']")).sendKeys("My Name");
driver.findElement(By.xpath("//input[#value='Bike']")).click();
driver.findElement(By.xpath("//input[#value='Car']")).click();
driver.findElement(By.xpath("//input[#value='Boat']")).click();
driver.findElement(By.xpath("//input[#value='male']")).click();
Thread.sleep(5000);
//Switch to window1(parent window) and performing actions on it.
driver.switchTo().window(window1);
driver.findElement(By.xpath("//option[#id='country6']")).click();
driver.findElement(By.xpath("//input[#value='female']")).click();
driver.findElement(By.xpath("//input[#value='Show Me Alert']")).click();
driver.switchTo().alert().accept();
Thread.sleep(5000);
//Once Again switch to window2(child window) and performing actions on it.
driver.switchTo().window(window2);
driver.findElement(By.xpath("//input[#name='fname']")).clear();
driver.findElement(By.xpath("//input[#name='fname']")).sendKeys("Name Changed");
Thread.sleep(5000);
driver.close();
//Once Again switch to window1(parent window) and performing actions on it.
driver.switchTo().window(window1);
driver.findElement(By.xpath("//input[#value='male']")).click();
Thread.sleep(5000);
}

Categories

Resources