javascript 如何点击量角器中的隐藏元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22265040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:48:19  来源:igfitidea点击:

How to click on hidden element in protractor?

javascriptcssangularjsselenium-webdriverprotractor

提问by Piyush Jajoo

I have an element which is visible only when I hover over it.

我有一个元素,只有当我悬停在它上面时才可见。

I've written following code to hove over the panel so that the element is visible.

我编写了以下代码以将鼠标悬停在面板上,以便元素可见。

ptor.actions().
            mouseMove(ptor.findElement(protractor.By.xpath('//*[@id="productapp"]/div/div/div[2]/div/div/div/div[2]/div/div/div/div[4]/table/thead/tr/th[2]'))).
            perform();
        ptor.element.all(by.tagName('i')).then(function(elm){
            elm[0].click();
        });

Now I tried to click on it, but it says - ElementNotVisibleError: element not visible error in protractor.

现在我试图点击它,但它说 - ElementNotVisibleError:量角器中的元素不可见错误。

Basic scenario is, I want to hover over a panel and then click on the hidden element, because the element is not visible until it is hovered over.

基本情况是,我想将鼠标悬停在面板上,然后单击隐藏的元素,因为该元素在悬停在其上方之前不可见。

采纳答案by Piyush Jajoo

Following code worked for me.

以下代码对我有用。

  ptor.actions().
    mouseMove(ptor.findElement(protractor.By.xpath('//*@id="productapp"]/div/div/di??v[2]/div/div/div/div[2]/div/div/div/div[4]/table/thead/tr/th[2]'))).perform();

   ptor.element.all(by.css('i.ng-scope.tea-ic-sorting')).then(function(elm){
       elm[0].click();
    });

回答by alecxe

Sometimes, there are cases when you intentionally want to click a hidden element.

有时,有时您会有意单击隐藏元素。



One option would be to click via javascript:

一种选择是通过 javascript单击:

var elm = element(by.id("myid"));
browser.executeScript("arguments[0].click();", elm.getWebElement());

See also: WebDriver click() vs JavaScript click()

另请参阅:WebDriver click() 与 JavaScript click()



Another, to make an element visibleand click it. Now, this depends on how the element was hidden - with a style.blockor style.visibilityor with the ng-hideetc. Sample solution where we set the element's visibilityto visibleand the displayto block:

另一个,使元素可见并单击它。现在,这取决于元素的隐藏方式 - 使用 astyle.blockstyle.visibilityorng-hide等。示例解决方案,我们将元素的visibilitytovisible和 the displayto设置为block

var elm = element(by.id("myid"));
browser.executeScript(function (arguments) {
    arguments[0].style.visibility = 'visible'; 
    arguments[0].style.display = 'block';
}, elm.getWebElement());