0
package captionlabs;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Login_captionlabs {
    public static void main(String[] args) {
         System.setProperty("webdriver.gecko.driver", "D:\\selenium\\geckodriver.exe");
        FirefoxDriver driver = new FirefoxDriver();
            driver.get("https://my.development.sourcefile.com");
            driver.findElement(By.xpath(".//*[@id='login-form']/fieldset/section[1]/label[2]/input")).sendKeys("[email protected]");
1
  • Trying with other locators like id, name
    – user24314
    May 24, 2017 at 7:58

2 Answers 2

1

When you are calling findElement the element probably doesn't exist yet on your page.

You need to wait explicitly for the element to become visible, here's how: https://stackoverflow.com/questions/20903231/how-to-wait-until-an-element-is-present-in-selenium

1

You are getting the "NoSuchElementException" because as soon as you enter the url you are using sendkeys even before the web page has completely loaded.

I would suggest After you open the url wait till the webelement that you are looking for to get loaded first and then use sendkeys

so the refactored code should look somewhat like this.

driver.get("https://my.development.sourcefile.com");

//Initializing the 'wait' with a 30 seconds deplay before it throws a NoSuchElementException
WebDriverWait wait = new WebDriverWait(driver,30);

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='login-form']/fieldset/section[1]/label[2]/input"));

driver.findElement(By.xpath(".//*[@id='login-form']/fieldset/section[1]/label[2]/input")).sendKeys("[email protected]");

Let me know if this helps you or not.

2
  • Code could be much cleaner by assigning the Wait return value to a variable instead of using the XPath twice.
    – FDM
    May 24, 2017 at 13:00
  • I agree but I wanted to keep it simple for the user. Thanks
    – Alok
    May 24, 2017 at 13:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.