Selenium Webdriver - click on hidden elements

前端 未结 5 1339
自闭症患者
自闭症患者 2020-11-27 16:58

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

相关标签:
5条回答
  • 2020-11-27 17:34

    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.

    0 讨论(0)
  • 2020-11-27 17:34

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

    0 讨论(0)
  • 2020-11-27 17:40

    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.

    0 讨论(0)
  • 2020-11-27 17:52

    You can give the following a try :

    ((JavascriptExecutor)driver).executeScript("$('.goog-menu.uploadmenu > input').click();");
    
    0 讨论(0)
  • 2020-11-27 17:55

    Simple solution:

    WebElement tmpElement = driver.finElement(ElementLocator);
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", tmpElement);
    
    0 讨论(0)
提交回复
热议问题