Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I am trying to automate upload file functionality in Google Drive. The element used to pass parameters is hidden with height - 0px. None of the user actions would make this element visible. So I need a workaround to click on the element while it is not visible.

<input type="file" style="height: 0px; visibility: hidden; position: absolute; width: 340px; font-size: inherit;" multiple=""/>

The XPath for the above element is -

*[@class='goog-menu goog-menu-vertical uploadmenu density-tiny']/input

I am using

WebDriver.findElement(By.xpath(<xpath>).sendKeys(<uploadFile>)

Exception -

org.openqa.selenium.ElementNotVisibleException

Element is not currently visible and so may not be interacted with.

I have tried using JavascriptExecutor. But unable to find the exact syntax.

1 Answer

0 votes
by (62.9k points)

Use this code:

WebElement elem = yourWebDriverInstance.findElement(By.xpath("//*[@class='goog-menu goog-menu-vertical uploadmenu density-tiny']/input"));

String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";

((JavascriptExecutor) yourWebDriverInstance).executeScript(js, elem);

The above code snippet would change the visibility of your file input control. You can then proceed with the usual steps for file upload like this:

elem.sendKeys("<LOCAL FILE PATH>"); 

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s automation testing course online

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...