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

前端 未结 6 2018
自闭症患者
自闭症患者 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: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)));
    }
    

提交回复
热议问题