Test Scenario: Trying to capture and test Gmail Login.
Current Output: Mozilla instance opens up. Username is entered but
the Password is not being entered by the WebDriver code.
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
FirefoxDriver varDriver=new FirefoxDriver();
varDriver.get("http://gmail.com");
WebElement webElem= varDriver.findElement(By.id("identifierId"));
webElem.sendKeys("error59878#gmail.com");
WebElement nextButton=varDriver.findElement(By.id("identifierNext"));
nextButton.click();
varDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement wePass=varDriver.findElement(By.cssSelector(".rFrNMe.P7gl3b.sdJrJc.Tyc9J"));
wePass.sendKeys("test1");
ElementNotInteractableException
As per the documentation, ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the DOM Tree, it is not in a state that can be interacted with.
Reasons & Solutions :
The reason for ElementNotInteractableException to occur can be numerous.
Temporary Overlay of other WebElement over the WebElement of our interest:
In this case, the direct solution would have been to induce ExplicitWait i.e. WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated as follows:
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();
A better solution will be to get a bit more granular and instead of using ExpectedCondition as invisibilityOfElementLocated we can use ExpectedCondition as elementToBeClickable as follows:
WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();
Permanent Overlay of other WebElement over the WebElement of our interest :
If the overlay is a permanent one in this case we have to cast the WebDriver instance as JavascriptExecutor and perform the click operation as follows:
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
Now addressing the error ElementNotInteractableException in this particular context we need to add ExplicitWait i.e. WebDriverWait as follows :
You need to induce a bit of wait for the Password field to be properly rendered in the HTML DOM. You can consider configuring an ExplicitWait for it. Here is the working code to login into Gmail using Mozilla Firefox:
System.setProperty("webdriver.gecko.driver","C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement email_phone = driver.findElement(By.xpath("//input[#id='identifierId']"));
email_phone.sendKeys("error59878#gmail.com");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[#name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("test1");
driver.findElement(By.id("passwordNext")).click();
Related
I've written some code to send input to a textbox as shown below :
However during execution, the webdriver returns ElementNotInteractableException even though it shows the input type is a textbox.
I've tried to send input using the following ways but have been unsuccessful, seeking advice, thanks!:
1)
driver.findElement(By.xpath("//input[#name='bkg_no']")).sendKeys("ABCDE9000333");
2)
String bkg = "ABCDE9000333"
WebElement bkgNo = driver.findElement(By.xpath("//input[#name='bkg_no']"));";
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value="+bkg+";", bkgNo);
3)
driver.findElement(By.name("bkg_no")).sendKeys("ABCDE9000333");
4)
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementByName('bkg_no').value='PKG900890300'");
To send a character sequence to the <input> you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("form[name='form'][onkeydown*='search'] div.wrap_search input.input[name='bkg_no']"))).sendKeys("ABCDE9000333");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//form[#name='form' and contains(#onkeydown, 'search')]//div[contains(#class, 'wrap_search')]//input[#class='input' and #name='bkg_no']"))).sendKeys("ABCDE9000333");
I cant locate a button on dialog pages, I tried to use cssselectors, xpaths, but simple i cant locate buttons/texts on modal dialogs.
I attached a screenshot from the code.
What can you recommend?
Thank you!
By.xpath(".//button[.='/"Submit/"'])
or
By.xpath(".//button[#class='btn btn-default'])
If it found but click doesnt work try that javascript from other comment
I presume you are able to identify the element.However unable to click on that.
Try use following options.
Use WebDriverWait and elementToBeClickable to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
elementBtn.click();
Use Action class to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
Actions action=new Actions(driver);
action.moveToElement(elementBtn).click().build().perform();
Java Script Executor to click on the element.
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default")));
Note: If above all options doesn't work.Check if there any iframe avaialable.If so, you need to switch to iframe first.like below.
driver.switchTo().frame("framename"); //name of the iframe.
driver.switchTo().frame(0); //you can use index as well.
You could try this:
JavascriptExecutor js= (JavascriptExecutor) driver;
WebElement webElement=driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default"));
js.executeScript(“arguments[0].click()”, webElement);
Hope it helps.
Try the bellow xpath:
driver.findElement(By.xpath("//div[#class='modal-footer']//button[contains(#class,'btn-default')]")).click();
I am learning how to automate tests with Selenium WebDriver, however I got stuck and cannot make dropdown menu to work in Firefox. The same code runs perfectly fine in Chrome.
The site I am practicing on is:
http://www.executeautomation.com/demosite/index.html
and I want to click the following item from menu: Automation Tools > Selenium > Selenium WebDriver.
The error message suggest that the web element may not be loaded on the screen yet, so I have implemented some method to wait with every execution until the element shows up:
public static void ImplicitWait(WebDriver driver){
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
but it did not helped.
Then I read that it is better to "pipe" those moveToElement() methods instead of performing them one by one. So I changed this:
action.moveToElement(menu).perform();
action.moveToElement(selenium).perform();
action.moveToElement(seleniumWebDriver).click().build().perform();
to one line. At this point it started to work on Chrome, but I am still struggling to make it work on Firefox.
The current code looks like this:
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver-v0.24.0-win64\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
ImplicitWait(driver);
driver.navigate().to("http://executeautomation.com/demosite/index.html");
WebElement menu = driver.findElement(By.id("Automation Tools"));
WebElement selenium = driver.findElement(By.id("Selenium"));
WebElement seleniumWebDriver = driver.findElement(By.id("Selenium WebDriver"));
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(selenium).moveToElement(seleniumWebDriver).click().build().perform();
As I mentioned above the same works fine when I switch to Chrome, but with Firefox I get the error message:
Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-9862, 206) is out of bounds of viewport width (1283) and height (699)
I am using:
* Firefox v66.0.2
* Java v1.8.0_201
* Selenium Java v3.141.59
* GeckoDriver v0.24.0
Please help.
The main issue with the Web Application is that the HTML DOM attains document.readyState equals to complete even before the sub-menu element with text as Selenium WebDriver gets rendered. Hence you see the error as:
Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-4899, 91) is out of bounds of viewport width (1366) and height (664)
Solution
So an ideal solution would be:
Induce WebDriverwait for the titleIs() Execute Automation
Induce WebDriverwait for the menu element with text as Automation Tools
Induce WebDriverwait for the sub-menu element with text as Selenium
Induce WebDriverwait for the sub-menu elementToBeClickable with text as Selenium
You can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MouseHoverFirefox {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.executeautomation.com/demosite/index.html");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleIs("Execute Automation"));
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#id='Automation Tools']")))).build().perform();
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#class='active has-sub']/a/span//following::ul[1]/li[#class='has-sub']/a/span[#id='Selenium']")))).build().perform();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#class='active has-sub']/a/span//following::ul[1]/li/a/span[#id='Selenium']//following::ul[1]/li/a/span[text()='Selenium WebDriver']"))).click();
}
}
Browser Snapshot:
Please try the below code(if you are inside frame , you need to come out and use below code):
WebDriver driver=new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,400)");
Use WebDriverWait and try the following code.
driver.get("http://executeautomation.com/demosite/index.html");
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement menu= wait.until(ExpectedConditions.elementToBeClickable(By.id("Automation Tools")));
Actions action = new Actions(driver);
action.moveToElement(menu).build().perform();
WebElement selenium =wait.until(ExpectedConditions.elementToBeClickable(By.id("Selenium")));
action.moveToElement(selenium).build().perform();
WebElement seleniumWebDriver =wait.until(ExpectedConditions.elementToBeClickable(By.id("Selenium WebDriver")));
action.moveToElement(seleniumWebDriver).click().build().perform();
Try using it -
action.moveToElement(menu).build().perform();
Thread.sleep(500);
moveToElement(selenium).build().perform();
Thread.sleep(500);
moveToElement(seleniumWebDriver).click().build().perform();
I've observed the same issue with geckodriver and Actions class. Although you can go with following code
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver-v0.24.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://executeautomation.com/demosite/index.html");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement mainmenu = driver.findElement(By.xpath("//li[#class='active has-sub']"));
WebElement submenu = driver.findElement(By.xpath("//li[#class='has-sub'] [contains(.,'Selenium')]"));
WebElement intendedLink = driver.findElement(By.xpath("//li[#class='has-sub'] [contains(.,'Selenium')]//li[contains(.,'Selenium WebDriver')]"));
Actions action =new Actions(driver);
action.moveToElement(mainmenu).clickAndHold().build().perform();
Thread.sleep(1000);
action.moveToElement(submenu).clickAndHold().build().perform();
Thread.sleep(1000);
intendedLink.click();
Code is working fine at my end. let me know if any issue.
Note: Keep the mouse pointer out of web page screen else it override the current focus.
I had to re-test the xpath, Previously it was working fine, But now it gives me an error.
I tried with different locators as well, Like id, name. but still get the same error.
package staging;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class login {
public static void main (String[]args){
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//opening the browser
driver.get("https://staging.keela.co/login");
//logging
driver.findElement(By.xpath("//*[#id='login-email']")).sendKeys("bandanakeela#yopmail.com");
driver.findElement(By.xpath("//*[#id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//*[#id='login-form']/div[3]/div/button")).click();
}
}
As you access the url https://staging.keela.co/login there is a Ajax loader which blocks the UI, so we have to wait for the Ajax loader to complete loading the all the WebElements and the email and password field becomes visible. To achieve that we will introduce ExplicitWait i.e. WebDriverWait with ExpectedConditions set to elementToBeClickable for the email field.Here is the working code block:
System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://staging.keela.co/login");
WebDriverWait wait = new WebDriverWait (driver, 15);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='login-email']")));
element.sendKeys("bandanakeela#yopmail.com");
driver.findElement(By.xpath("//input[#id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//button[#class='btn btn-sm btn-block btn-primary']")).click();
Try this below code.
Note: If id attribute is available then you should use id and for xpath try to use relative xpath.
I have used explicit wait method, so your driver may able to find the next webelement, after page is fully loaded.
driver.get("https://staging.keela.co/login");
driver.manage().window().maximize();
//Explicit wait for 60 seconds, to find the webelement. You can increase or decrease the time as per your specification.
new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("login-email"))));
driver.findElement(By.id("login-email")).sendKeys("bandanakeela#yopmail.com");
driver.findElement(By.id("login-password")).sendKeys("keela");
driver.findElement(By.xpath("//button[#type='submit'][text()='Log in']")).click();
The page is using js to construct elements. So I would suggest you to use phantomjs driver.
Then you have to wait until element exist. You see the gear icon when page is loading. wait until the element loads. and also you can use id instead of xpath since you know your element id .
You can choose which wait type you want to use. Explicit Waits or Implicit Waits.
Here is the selenium documentation.
and example code for wait:
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("login-email")));
or you can wait until page load:
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
You are opening the URL and at the very next moment entering email-id. Before entering email-id, you need to check if the page is fully loaded. In this case, explicit wait will help you out-
//opening the browser
driver.get("https://staging.keela.co/login");
//Explicit wait
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement email;
email = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-email")));
//logging
driver.findElement(By.xpath("//*[#id='login-email']")).sendKeys("bandanakeela#yopmail.com");
driver.findElement(By.xpath("//*[#id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//*[#id='login-form']/div[3]/div/button")).click();
To upload image on the field in this URL: http://demoqa.com/registration/
I used the following code that works on chrome but not on Firefox. May I please know the reason and solution?
WebDriver driver;
System.setProperty("webdriver.gecko.driver","C:\\Users\\abc\\Desktop\\Selenium\\geckodriver-v0.17.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://demoqa.com/registration/");
WebElement elementUpload=driver.findElement(By.xpath("//*[#id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:\\roboraid.jpg");
Error shown here is:
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: File not found: C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg
try this code with xpath with Explicit wait:
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com/registration/");
driver.manage().window().maximize();
WebElement elementUpload=driver.findElement(By.xpath("//*[#id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:\\1504857398686.png");
Please find output image:
try replacing spaces in the file location
from
C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg
to
C:\Users\Public\Pictures\Sample_Pictures\Chrysanthemum.jpg
Hope this works
Change the xpath to the below one and try. It worked for me.
//input[#name='profile_pic_10' and #type='file']
May I know the browser and environment details which you have tried?
Actually it was the issue with geckodriver of firefox
refer :-
https://github.com/mozilla/geckodriver/issues/659
You need to update your geckodriver
OR
Use chromedriver as below
System.setProperty("webdriver.chrome.driver", "D:\\Workspace\\StackOverlow\\src\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver( );
driver.get("http://demoqa.com/registration/");
driver.manage().window().maximize();
((JavascriptExecutor) driver).executeScript("scroll(0,300)");
WebElement elementUpload=driver.findElement(By.xpath("//*[#id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:"+File.separator+"22.jpeg");