I'm using Webdriver in Java to login into Instagram:
WebDriver driver = new ChromeDriver();
//open instagram.com:
driver.get("https://www.instagram.com");
//type my_username in the user text box
WebElement user = driver.findElement(By.xpath("//*[@id=\"loginForm\"]/div/div[1]/div/label/input"));
user.sendKeys("my_username");
Webdriver opens instagram but I receive this error (and the text field is not filled):
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [12eccee1a2fd0dbabc426dca6651ef75, findElement {using=xpath, value=//*[@id="loginForm"]/div/div[1]/div/label/input}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 101.0.4951.54, chrome: {chromedriverVersion: 101.0.4951.41 (93c720db8323..., userDataDir: /var/folders/_y/x_dzvzfj1vs...}, goog:chromeOptions: {debuggerAddress: localhost:53345}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), se:cdp: ws://localhost:53345/devtoo..., se:cdpVersion: 101.0.4951.54, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 12eccee1a2fd0dbabc426dca6651ef75
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:567)
at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162)
at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:60)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:385)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:377)
Thanks!
CodePudding user response:
Try to change double quotes for single quotes on loginForm xpath:
WebElement user = driver.findElement(By.xpath("//*[@id='loginForm']/div/div[1]/div/label/input"));
user.sendKeys("my_username");
If you want you can simplify this code, not using the variable user:
string XPath = "//*[@id='loginForm']/div/div[1]/div/label/input";
driver.findElement(By.xpath(XPath)).sendKeys("my_username");
CodePudding user response:
Problem was that I was sending the text to fast, before the text box was even available. Some check and wait mechanism should be used before accesing an element.