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]");
-
Trying with other locators like id, name– user24314May 24, 2017 at 7:58
2 Answers
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
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.
-
Code could be much cleaner by assigning the Wait return value to a variable instead of using the XPath twice.– FDMMay 24, 2017 at 13:00
-