24

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 work around 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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
praneel
  • 1,842
  • 4
  • 19
  • 24
  • Take a look at the HTML, is Google Drive just a form? If so, don't use Selenium, just write a script to post a form with data you specify. – jasop Sep 11 '12 at 05:06

5 Answers5

24

Try this:

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 bunch would change the visibility of your file input control. You can then proceed with the usual steps for file upload like:

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

Be aware, by changing the visibility of an input field you are meddling with the application under test. Injecting scripts to alter behavior is intrusive and not recommended in tests.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Ashwin Prabhu
  • 9,285
  • 5
  • 49
  • 82
14

Simple solution:

WebElement tmpElement = driver.finElement(ElementLocator);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", tmpElement);
TheodorosPloumis
  • 2,396
  • 1
  • 17
  • 31
Karthikeyan
  • 2,634
  • 5
  • 30
  • 51
2

Try this sample code:

JavascriptExecutor executor= (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('ID').style.display='block';");
Select select = new Select(driver.findElement(By.id("ID")));
select.selectByVisibleText("value");
Thread.sleep(6000);

By using java script executor and make the element visible then click on the element through ID. Hope it hepls..

testing
  • 1,736
  • 15
  • 46
  • 75
1

Try this:

WebElement elem = yourWebDriverInstance.findElement(
   By.cssSelector(".uploadmenu > input"));
String js = 
  "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((JavascriptExecutor) yourWebDriverInstance).executeScript(js, elem);

Here I have replaced XPath by CSS Selector. Let me know Is the above scripting is working or not.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
user3487861
  • 340
  • 2
  • 2
1

You can give the following a try:

((JavascriptExecutor)driver).executeScript("$('.goog-menu.uploadmenu > input').click();");
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Amith
  • 6,818
  • 6
  • 34
  • 45