如何使用Selenium WebDriver悬停并单击不可见元素? [英] How to Hover over and click on an invisible element using selenium webdriver?

查看:642
本文介绍了如何使用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

  1. 将鼠标悬停在元素上
  2. 单击元素(它将显示4个选项)
  3. 单击选项之一

我正在将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();

  1. MainMenuBTN =鼠标悬停时可见的元素
  2. subMenuBTN =从菜单选项中选择的元素 显示的
  1. MainMenuBTN = element that becomes visible when you hover the mouse over it
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆