Exception in thread “main” org.openqa.selenium.NoS

2019-02-27 17:57发布

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();       
 }
}

4条回答
祖国的老花朵
2楼-- · 2019-02-27 18:09

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"));
查看更多
Luminary・发光体
3楼-- · 2019-02-27 18:24

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();
查看更多
三岁会撩人
4楼-- · 2019-02-27 18:25

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(); 
查看更多
祖国的老花朵
5楼-- · 2019-02-27 18:34

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();
查看更多
登录 后发表回答