5

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("[email protected]");
        driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela");
        driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click();       
 }
}
4
  • Are you sure the id is login-email? Could be a typo.. do you have a screenshot of the source code for the page you are testing? Commented Sep 13, 2017 at 16:14
  • ok done I have posted it u can see it at left side(top) @jaredgilmore Commented Sep 13, 2017 at 16:21
  • Sorry, I meant the source code of the web page you're testing against, not the test. Commented Sep 13, 2017 at 16:22
  • added that too @jaredgilmore Commented Sep 13, 2017 at 16:24

4 Answers 4

5

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("[email protected]");
driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//button[@class='btn btn-sm btn-block btn-primary']")).click(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please explain, why are you using xpath locator here. If id attribute is present. This does not make any sense for me..!!! and for your kind information id attribute is one of the fastest locator to finding a webelement.
1

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("[email protected]");
driver.findElement(By.id("login-password")).sendKeys("keela");
driver.findElement(By.xpath("//button[@type='submit'][text()='Log in']")).click();

1 Comment

Its not working..same error like :Unable to locate element: #login\-email
0

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"));

2 Comments

Thanks its working but only in part of sendkeys..data are provided but login button is not clicked
I just added the wait examples , that is why :) You were unable to see the elements and your question's answer was waiting until element is exist.
0

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("[email protected]");
driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.