简体   繁体   English

如何使用 selenium webdriver 悬停并单击不可见的元素?

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

There is an invisible element on my HTML page which becomes visible when a mouse hover is done on the element.我的 HTML 页面上有一个不可见的元素,当鼠标悬停在该元素上时,该元素就会变得可见。 What I Have to do is我要做的是

  1. Hover over the element将鼠标悬停在元素上
  2. Click on the element (it will display 4 options)单击元素(它将显示 4 个选项)
  3. Click on one of the options单击其中一个选项

I am using Java API for selenium web driver and following is what I have been trying我正在为 selenium 网络驱动程序使用 Java API,以下是我一直在尝试的

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

subMenuBTN.click();
  1. MainMenuBTN = element that becomes visible when you hover the mouse over it MainMenuBTN = 将鼠标悬停在其上时变为可见的元素
  2. subMenuBTN = element that is being chosen out of the menu options that are displayed subMenuBTN = 从显示的菜单选项中选择的元素

What is happening is, the click() on MainMenuBTN is generating ElementNotVisible exception.发生的事情是,MainMenuBTN 上的 click() 正在生成 ElementNotVisible 异常。 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 Note: mainMenuBTN and subMenuBTN are WebElements generated by注意:mainMenuBTN 和 subMenuBTN 是由

driver.findElement(By.xpath("xpath_string"))

Am I missing anything?我错过了什么吗? Help appreciated !帮助赞赏!

using javascript executor like使用 javascript 执行器

((JavascriptExecutor) webdriver).executeScript("document.getElementById('btn').click();");

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.您需要将光标移动到 mainMenuBTN(这是可见的,而不是当您将鼠标悬停在它上面时变得可见的元素),然后显示您需要单击的 subMenuBTN。

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.根据我对原始代码的唯一编辑是添加一条语句,在您单击它之前将光标移动到您的 subMenuBTN。 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.如果是这种情况,请告诉我。

Your Actions builder looks slightly wrong to me.在我看来,您的 Actions 生成器有点不对劲。 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();    
}
Actions builder = new Actions(driver);
builder.MoveToElement(menu).MoveToElement(submenu).Click().Perform();

It works under Chrome, but doesn't work in FF它在 Chrome 下工作,但在 FF 中不起作用

You could try this:你可以试试这个:

  1. Get your WebElement by its xpath.通过它的 xpath 获取您的 WebElement。
  2. Hover element.悬停元素。
  3. Get your WebElement by its xpath again.再次通过其 xpath 获取您的 WebElement。
  4. Click it.点击它。

It's because of element's id is changing when you hover over it and you should find it again.这是因为当您将鼠标悬停在它上面时,元素的 id 会发生变化,您应该再次找到它。

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.我使用此方法对元素的实例化进行 ipmlement 控制显式等待。

protected WebElement getWebEl(String xpath, int waitSeconds) {
    wait = new WebDriverWait(driver, waitSeconds);
    return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
}

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.我的情况是我们有一个行表,如果鼠标悬停在该行上,其中一列 (td) 应该显示大约 4 个图标,我们应该点击它。

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

It worked for me.它对我有用。 moveToELement() move your control to the element moveToELement()将控件移动到元素

clickAndHold() clicks and holds the hovered element, so that its easy for us to do operation on visible elements. clickAndHold()点击并按住悬停的元素,方便我们对可见元素进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 selenium webdriver 将鼠标悬停在不可见按钮上? - How Can I Hover over on an invisible button using selenium webdriver? 将鼠标悬停在元素上,然后使用Java等待Selenium WebDriver - Hover over on element and wait with Selenium WebDriver using Java 如何使用Selenium Webdriver滚动下拉列表并选择不可见/隐藏元素? - How to scroll the dropdown and select the invisible/hidden element using selenium webdriver? 如何使用Java使用Selenium WebDriver访问不可见的无序列表元素 - How to access invisible Unordered List element with Selenium WebDriver using Java 如何使用Selenium Webdriver单击元素 - How to click on the element using Selenium Webdriver 如何使用 JavaScript 单击 Selenium WebDriver 中的元素? - How to click an element in Selenium WebDriver using JavaScript? 在使用Selenium和Java将鼠标悬停在ebay.com中的元素上之后,如何单击可见的元素 - How to click on an element which is visible after mouse hover over an element within ebay.com using Selenium and Java 如何使用Java从Selenium WebDriver中的不可见下拉元素中选择选项 - How to select option from invisible drop-down element in Selenium WebDriver using Java 如何鼠标悬停并单击webdriver中的元素 - How to mouse hover and click on element in webdriver 元素对于悬停可见,但对于单击不可见 - Element visible for hover but invisible for click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM