繁体   English   中英

FindElements不会返回所有匹配的元素

[英]FindElements is not returning all matched elements

我正在使用Selenium C#。 这是我正在搜索的html(请原谅拼写-这不是转录错误):

<td class="Search3-product-cell" align="left">
  <div class="SearchRersultsNameCell">
    <a id="MainPlaceHolder_ContentPlaceHolder_SearchMatrix_SearchResultView_ProductNameLink_33" class="Name">Tango 6 Pc. Queen Bedroom Set</a>
    <br/>
    <a id="MainPlaceHolder_ContentPlaceHolder_SearchMatrix_SearchResultView_ProductPriceLink_33">$1,999.00</a>
  </div>
</td>

我有一个IWebElement对td元素的引用(x)。 但是我无法“看到”其中的第二个锚元素。 我尝试了两种主要方法。

方法1:

foreach (IWebElement we in x.FindElements(By.TagName("a"))) // for each anchor element
{
     if (we.GetAttribute("class").Equals("Name"))
     {
        name = we.Text;
     }
     else
     {
        price = Util.ConvertCurrencyToDecimal(we.Text);
     }
}

使用此代码,它将永远不会看到第二个锚(没有'class =“ Name”'的锚)。

第二种方法是:

IWebElement x = elem.FindElement(By.ClassName("SearchRersultsNameCell"));
myLocator = By.CssSelector("a[id^='MainPlaceHolder_ContentPlaceHolder_SearchMatrix_SearchResultView_ProductNameLink_']");
if (SeleniumHelpers.IsElementPresentNoWait(elem, myLocator))
{
    name = x.FindElement(myLocator).Text;
}
else
{
    name = "Name not found";
}

myLocator = By.CssSelector("a[id^='MainPlaceHolder_ContentPlaceHolder_SearchMatrix_SearchResultView_ProductPriceLink_']");
if (SeleniumHelpers.IsElementPresentNoWait(elem, myLocator))
{
    price = x.FindElement(myLocator).Text;
}
else
{
    price = -1;
}

同样,在任何情况下,代码都不会看到第二个锚。

我想念什么? 提前致谢。

在:

if (we.GetAttribute("class").Equals("Name"))

如果属性不存在,则GetAttribute()将返回null 在此示例中,第二a标签没有class属性,并返回null null上调用Equals()导致null指针异常,至少导致for循环退出。

有关GetAttribute()方法的文档: http : GetAttribute()

我深表歉意。 事实证明,硒工作正常-并返回所有元素。 问题是运行自动化测试时使用的配置与我手动运行浏览器的方式之间存在细微差异,导致浏览器页面内容不同。 很抱歉浪费人的时间! 给我上色尴尬。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM