硒找到了元素(锚),但仍然说元素不可见? [英] Selenium finds the element (anchor) but still says element not visible?

查看:37
本文介绍了硒找到了元素(锚),但仍然说元素不可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清除所有"按钮,它是一个锚点.HTML结构是这样的:

I have a "clear all" button which is an anchor. The HTML structure is like this:

<div class="form-row toggle-closed" id="ProductFilters" style="display: block;">
    <div class="form-row__filter">
        <ul class="form-row__filter__bg-display">
            <li class="filter__group__item__small">
                <a id="ProductFiltersFilterText" class="f-right" data-select-all="ProductFilters" href="#">clear all</a>
            </li>
        </ul>
    </div>  
</div>

然后在Selenium测试中,我试图使用以下方法找到 a 标记:

Then in the Selenium test I'm trying to find the a tag using this:

SeleniumHelper.ExpandFilterSection(_webDriver, "#ProductFilters");
var clearAllButton = _webDriver.FindElement(By.CssSelector("div.form-row__filter>ul>li>#ProductFiltersFilterText"));
clearAllButton.Click();

然后我开始调试,在自动化的Chrome窗口中,我看到通过执行 ExpandFilterSection ,过滤器被展开,清除所有"按钮被暴露,然后出现一个错误:

THen I started debugging, in the automated Chrome window I could see that by executing ExpandFilterSection, the filter was expanded, the "clear all" button was exposed, then a bug said:

OpenQA.Selenium.ElementNotVisibleException: 'element not visible'

我在汽车上看到的情况如何:

How ever in the Autos I see:

似乎找到了全部清除"按钮,为什么它显示元素不可见"?不过,按钮的功能是由JavaScript触发的.

It seems that the "clear all" button is found, why does it say "element not visible"? Functionality of the button is trigger by JavaScript though.

推荐答案

要对元素进行 click(),文字为清除所有,您必须诱导WebDriverWait 作为所需的 ElementToBeClickable(),您可以使用以下

To click() on the element with text as clear all you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:

  • linkText :

SeleniumHelper.ExpandFilterSection(_webDriver, "#ProductFilters");
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.LinkText("clear all"))).Click();

  • cssSelector :

    SeleniumHelper.ExpandFilterSection(_webDriver, "#ProductFilters");
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div#ProductFilters>div.form-row__filter>ul.form-row__filter__bg-display>li.filter__group__item__small>a#ProductFiltersFilterText"))).Click();
    

  • xpath :

    SeleniumHelper.ExpandFilterSection(_webDriver, "#ProductFilters");
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@id='ProductFilters']/div[@class='form-row__filter']/ul[@class='form-row__filter__bg-display']/li[@class='filter__group__item__small']/a[@id='ProductFiltersFilterText']"))).Click();
    

  • 这篇关于硒找到了元素(锚),但仍然说元素不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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