相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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="; 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.

    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, 2012 at 5:06
    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.

    WebElement tmpElement = driver.finElement(ElementLocator);
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", tmpElement);
    
    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..

    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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.