[英]FindElements is not returning all matched elements
I am using Selenium C#. 我正在使用Selenium C#。 Here is the html that I'm searching (please excuse the spelling - it is not a transcription error): 这是我正在搜索的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>
I have an IWebElement reference (x) to the td element. 我有一个IWebElement对td元素的引用(x)。 But I have not been able to 'see' the second anchor element inside it. 但是我无法“看到”其中的第二个锚元素。 I have tried two primary ways. 我尝试了两种主要方法。
Method 1: 方法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);
}
}
With this code, it never sees the second anchor (the one without 'class="Name"'). 使用此代码,它将永远不会看到第二个锚(没有'class =“ Name”'的锚)。
The second method is: 第二种方法是:
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;
}
Again, in neither case does the code see the second anchor. 同样,在任何情况下,代码都不会看到第二个锚。
What am I missing?? 我想念什么? Thanks in advance. 提前致谢。
In: 在:
if (we.GetAttribute("class").Equals("Name"))
GetAttribute()
will return null
if the attribute doesn't exist. 如果属性不存在,则GetAttribute()
将返回null
。 In this example, the second a
tag doesn't have the class
attribute and returns null
. 在此示例中,第二a
标签没有class
属性,并返回null
。 Calling Equals()
on null
results in a null pointer exception, causing at least the for loop to exit. 在null
上调用Equals()
导致null指针异常,至少导致for循环退出。
Documentation on the GetAttribute()
method: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute(java.lang.String) 有关GetAttribute()
方法的文档: http : GetAttribute()
)
My profound apologies. 我深表歉意。 It turns out Selenium is working correctly - and returning all elements. 事实证明,硒工作正常-并返回所有元素。 The problem was there was a subtle difference in the configuration used when running the automated tests and how I was running the browser manually, resulting in different browser page content. 问题是运行自动化测试时使用的配置与我手动运行浏览器的方式之间存在细微差异,导致浏览器页面内容不同。 Sorry for wasting people's time! 很抱歉浪费人的时间! Color me embarrassed. 给我上色尴尬。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.