I am using Selenium C#. Here is the html that I'm searching (please excuse the spelling - it is not a transcription error):
<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. But I have not been able to 'see' the second anchor element inside it. I have tried two primary ways.
Method 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"').
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. In this example, the second a
tag doesn't have the class
attribute and returns null
. Calling Equals()
on null
results in a null pointer exception, causing at least the for loop to exit.
Documentation on the GetAttribute()
method: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute(java.lang.String)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.