如何使用Selenium WebDriver悬停并单击不可见元素? [英] How to Hover over and click on an invisible element using selenium webdriver?
问题描述
我的HTML页面上有一个不可见的元素,当鼠标悬停在该元素上时,该元素将变为可见.
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
- 将鼠标悬停在元素上
- 单击元素(它将显示4个选项)
- 单击选项之一
我正在将Java API用于Selenium Web驱动程序,以下是我一直在尝试的
I am using Java API for selenium web driver and following is what I have been trying
Actions builder = new Actions(driver);
builder.moveToElement(MainMenuBTN).click().build().perform();
subMenuBTN.click();
- MainMenuBTN =鼠标悬停时可见的元素
- subMenuBTN =从菜单选项中选择的元素 显示的
- MainMenuBTN = element that becomes visible when you hover the mouse over it
- subMenuBTN = element that is being chosen out of the menu options that are displayed
正在发生的是,MainMenuBTN上的click()正在生成ElementNotVisible异常. 为了避免这种情况,我尝试了以下操作,但是没有用.
What is happening is, the click() on MainMenuBTN is generating ElementNotVisible exception. I tried following to avoid this, but did not work.
Actions builder = new Actions(driver);
builder.moveToElement(mainMenuBTN).build().perform();
builder.click();
subMenuBTN.click();
A注意:mainMenuBTN和subMenuBTN是由以下元素生成的WebElements
A Note : mainMenuBTN and subMenuBTN are WebElements generated by
driver.findElement(By.xpath("xpath_string"))
我错过了什么吗?帮助赞赏!
Am I missing anything? Help appreciated !
推荐答案
使用
((JavascriptExecutor) webdriver).executeScript("document.getElementById('btn').click();");
这篇关于如何使用Selenium WebDriver悬停并单击不可见元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!