How to Hover over and click on an invisible element using selenium webdriver?

前端 未结 6 2014
自闭症患者
自闭症患者 2021-02-02 17:43

There is an invisible element on my HTML page which becomes visible when a mouse hover is done on the element. What I Have to do is

  1. Hover over the element
  2. <
相关标签:
6条回答
  • 2021-02-02 18:15

    Your Actions builder looks slightly wrong to me. Here is a example I use:

    public static void mouseClickByLocator( String locator ) {    
      WebElement el = driver.findElement( By.cssSelector( locator ) );    
      Actions builder = new Actions(driver);    
      builder.moveToElement( el ).click( el );    
      builder.perform();    
    }
    
    0 讨论(0)
  • 2021-02-02 18:20

    My case we had a table of rows, if mouse over on the row, one of the column(td) should display some 4 icons, we should click on it.

    Action action=new Action(driver);
    action.moveToElement(hoverElt).clickAndHold().build().perform();
    

    It worked for me. moveToELement() move your control to the element

    clickAndHold() clicks and holds the hovered element, so that its easy for us to do operation on visible elements.

    0 讨论(0)
  • 2021-02-02 18:26

    using javascript executor like

    ((JavascriptExecutor) webdriver).executeScript("document.getElementById('btn').click();");
    
    0 讨论(0)
  • 2021-02-02 18:28
    Actions builder = new Actions(driver);
    builder.MoveToElement(menu).MoveToElement(submenu).Click().Perform();
    

    It works under Chrome, but doesn't work in FF

    0 讨论(0)
  • 2021-02-02 18:29

    Well, after going through your questions numerous times and changing my answers many times I will go with -

    Issue - what I got from the original code -

    You need to move the cursor to the mainMenuBTN (which is visible not the element that becomes visible when you hover the mouse over it ) and subMenuBTN is then displayed which you need to click.

    The only edit to your original code as per me will be adding a statement to move the cursor to your subMenuBTN before you click it. This way works fine for me when I need to click sub menu item.

    Actions builder = new Actions(driver);
    builder.moveToElement(mainMenuBTN).build().perform();
    builder.moveToElement(subMenuBTN).build().perform();
    subMenuBTN.click();
    

    Please let me know if this is the case.

    0 讨论(0)
  • 2021-02-02 18:33

    You could try this:

    1. Get your WebElement by its xpath.
    2. Hover element.
    3. Get your WebElement by its xpath again.
    4. Click it.

    It's because of element's id is changing when you hover over it and you should find it again.

    Actions builder = new Actions(driver);
    
    WebElement mainMenuBTN = getWebEl("xpath_string",5);
    builder.moveToElement(mainMenuBTN).perform();
    mainMenuBTN = getWebEl("xpath_string",5);
    builder.click(mainMenuBTN);
    

    I use this method to ipmlement controlled explicit wait into my elements' instantiation.

    protected WebElement getWebEl(String xpath, int waitSeconds) {
        wait = new WebDriverWait(driver, waitSeconds);
        return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
    }
    
    0 讨论(0)
提交回复
热议问题