[Selenium UI] -> How to do file upload from System and handling InvalidElementStateException in Webdriver?
Whenever there is a use case of file upload from the system to your application, you should always check whether the DOM is having an element of the "Input" tag whose type is "File". If there is such an element available, you need to make sure that the element is present and you just need to pass the "File path" from your system to this element as a string.
For Example: If we need to tweet an image from our system to twitter, We can check "Is there any input tag available with a type file".
<input accept="image/jpeg,image/png,image/webp,image/gif,video/mp4,video/quicktime" multiple="" tabindex="-1" type="file" class="r-8akbif r-orgf3d r-1udh08x r-u8s1d r-xjis5s r-1wyyakw" data-testid="fileInput">
You will see that, there is an element available with an input field whose type is "File". Now, in your selenium script, you need to pass the path of your file.
imagePath = driver.find_element(By.XPATH, "//*[@data-testid='fileInput']")
imagePath.send_keys("C:\\Users\\username\\Documents\\Personal\\selenium.png")
It will upload the file to Twitter for the tweet.
If while doing this activity, you see InvalidElementStateException, it means the element is not clickable.
So you need to make sure, that the element is in active/clickable status before interacting with it.
WebDriverWait(driver,20).until(EC.element_to_be_clickable("element")).send_keys("C:\\Users\\username\\Documents\\Personal\\selenium.png")
The same can be done in Java:
new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.elementToBeClickable(By.xpath(""))).sendKeys("imagePath");
This will resolve the issue and will upload the file.
Note: In both cases, it will not open any windows dialogue. So don't get confused that you need to use "AutoIT", "Robot Class" or any other hack to upload files there.
#invalidSateException #fileUploadInSelenium #HowToUploadFileInSelenium