3

I am having trouble trying to upload a file using Selenium. I am not able to use the sendKeys() function to pass the path due to the browse button not having an input tag. It's written in Angular.

Here's the browse button element:

<a id="attachmentUpload-browse0" name="attachmentUpload-browse0" ng-click="clickBrowse($index)" class="btn-pri" xpath="1"></a>

I did find an input tag right below the code above in the source, but I am getting errors when trying to use .sendKeys().

<input type="file" id="attachmentUpload-file0" name="attachmentUpload-file0" fileread="$parent.attachments[$index].fileData" class="attachmentUpload-inputfile ng-isolate-scope" onchange="angular.element(this).scope().onFileChange(this)" style="" xpath="1">

Edit: Error when using .sendKeys() to input tag

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //input[@id="attachmentUpload-file0"]// because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@id="attachmentUpload-file0"]//' is not a valid XPath expression.
*** Element info: {Using=xpath, value=//input[@id="attachmentUpload-file0"]//}
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.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:319)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:421)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:311)
at com.ibm.esh.o2c.art.Tester.runTest(Tester.java:61)
at com.ibm.esh.o2c.art.Tester.main(Tester.java:70)

Error when using a tag for attachmentUpload-browse0:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
7
  • Update the question with the error stack trace you see while using the <input> tag Commented Apr 18, 2018 at 20:13
  • Will do. May take a bit. Selenium is acting very strange over past hour, page loads are taking forever. I see "Connecting" on bottom of browser for 2 minutes (loads in Chrome normally nearly instant). Was not having that issue in the days prior. Commented Apr 18, 2018 at 20:15
  • Okay got it up there now. Commented Apr 18, 2018 at 20:31
  • 1
    Try to use only driver.findElement(By.xpath("//input[@id='attachmentUpload-file0']")); element.sendKeys("test"); Commented Apr 18, 2018 at 20:43
  • 1
    That appeared to work. driver.findElement(By.xpath("//input[@id='attachmentUpload-file0']")).sendKeys(System.getProperty("user.dir")+"/test1.csv"); Thank you so much. Commented Apr 18, 2018 at 21:03

1 Answer 1

3

This error message...

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //input[@id="attachmentUpload-file0"]// because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@id="attachmentUpload-file0"]//' is not a valid XPath expression.

...implies that your XPath expression was not a valid one.

You can use the following line of code:

driver.findElement(By.xpath("//input[@id='attachmentUpload-file0']")).sendKeys("test");
Sign up to request clarification or add additional context in comments.

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.