I'm trying to reference a link element in the HTML below.
The element I'm trying to reference is the one with 'WANT_TO_FIND_THIS_LINK' in it.
Using this finds the main div just fine:
var productHolder = driver.FindElements(By.XPath("//div[@data-partnumber='123456']"));
But when I try to get to the link it returns nothing. I've tried a number of different ways, but they all return nothing:
var productHolder = driver.FindElements(By.XPath("//div[@data-partnumber='123456']//div[@class='fc-image image-fit']//a"));
var productHolder = driver.FindElements(By.XPath("//div[@data-partnumber='123456']//div[contains(@class, 'fc-image')]"));
var productHolder = driver.FindElements(By.XPath("//div[@data-partnumber='123456']//div[@class='fc-image image-fit']//a"));
But when I go back to the top with:
var productHolder = driver.FindElements(By.XPath("//div[@data-partnumber='123456']"));
it works fine.
Trying everything here: http://xpather.com/ it finds what I need. What am I doing wrong, any help would be great!
HTML code snippet:
<div class="col-sm-3 col-xs-6 fg-products-product" data-partnumber="123456" data-kjs-app="products/product" data-kjs-entityid="71">
<input type="hidden" data-kjs-setting="initialize" value="1" data-kjs-datatype="bool">
<input type="hidden" data-kjs-setting="hideDescription" value="1" data-kjs-datatype="bool">
<input type="hidden" data-kjs-setting="partnumber" value="123456">
<input type="hidden" data-kjs-setting="bridgeKey" value="search/result/main-13-getProduct|type:products|aggregationName:pro">
<div data-kjs-view="main">
<div class="fc-wrapper">
<div data-kjs-view="wishlist">
<div data-kjs-app="wishlist/control" class="fg-wishlist-control" data-kjs-entityid="183">
<input type="hidden" data-kjs-setting="partnumber" value="123456">
<input type="hidden" data-kjs-setting="onWishlist" value="0" data-kjs-datatype="bool">
<div data-kjs-view="controls">
<a href="javascript:void(0);" class="fc-add" data-kjs-click="add" title="Add to wishlist"></a>
</div>
</div>
</div>
<div class="fc-image image-fit">
<a data-kjs-click="linkClicked" href="/uk/professional/maincat/subcat/producta.html">WANT_TO_FIND_THIS_LINK
<img src="/mam/123456/mainproduct/160440/d4.jpg" alt="main image"></a>
</div>
<div data-kjs-view="addedValue">
</div>
<div class="fc-details">
<h6 class="fc-label" style="min-height: 78px;">
<a data-kjs-click="linkClicked" id="producttitle-71-10516762" href="/uk/professional/maincat/subcat/producta.html">
Fun Toy
</a>
</h6>
<div class="fc-price" data-kjs-view="price" style="min-height: 20px;">
<div class="fg-products-price" data-kjs-app="products/price" data-kjs-entityid="184">
<input type="hidden" data-kjs-setting="price.price" value="">
<input type="hidden" data-kjs-setting="price.priceFormatted" value="">
<input type="hidden" data-kjs-setting="price.oldPrice" value="0">
<input type="hidden" data-kjs-setting="price.oldPriceFormatted" value="">
<input type="hidden" data-kjs-setting="price.recommendedRetailPrice" value="">
<input type="hidden" data-kjs-setting="price.recommendedRetailPriceFormatted" value="">
<input type="hidden" data-kjs-setting="price.priceSaving" value="">
<div data-kjs-view="price" style="min-height: 20px;">
</div>
</div>
</div>
<div class="fc-ratings">
<a data-kjs-click="linkClicked" href="/uk/professional/maincat/subcat/producta.html#ratings">
<span class="ff-cursor-overlay"></span>
</a>
</div>
<div class="fc-description">
</div>
<div class="fc-compare" data-kjs-app="products/comparison/control" data-kjs-entityid="185">
<input type="hidden" data-kjs-setting="productId" data-kjs-value="10516762">
<div class="fg-checkbox-custom" data-kjs-app="content/checkbox" data-kjs-entityid="186">
<input id="compare_10516762" type="checkbox" class="fc-checkbox-custom" data-kjs-change="products/comparison/control|toggleComparison">
<label for="compare_10516762">
Compare
</label>
</div>
</div>
</div>
<div data-kjs-view="button">
</div>
<div data-kjs-view="lowestPriceInfo">
</div>
</div>
</div>
</div>
Given the provided HTML, this CSS selector will find that A tag.
div[data-partnumber='123456'] div.fc-image a
If you must have an XPath, the equivalent to the above would be
//div[@data-partnumber='123456']//div[contains(@class,'fc-image')]/a
NOTE: If these still return nothing, you need to make sure to add a WebDriverWait
and wait for clickable.