如何点击标记名为<a class="" <="" a=""> using Java in Selenium WebDriver

时间:2015-04-30 10:24:40

标签: java selenium

I have below HTML code, where I have different <'li' class> tags under the <'ul'> tag. Within the <'li'> class tag, I will have tag which needs to be clicked. These are results of some search in webpage, where all the items will be displayed as a list with pagination.

I need to verify if this exist? and if it exists in 3 or 4th Index on the page, I need to click that link. Please let me know how I can get all these items into array and loop them and click any product available at random index.

<ul id ="e1ResultsList">

<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<div class="banner">
<div class="text">High Offer</div>
</div>
</div>

<span class="e1ListingImages">
<a class="`jsListingURL`" `href`="/event?`eventid`=7844">
<div class="e1SellerLogo e1Hidden"></div>
</span>
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/event?`eventid`=7844">vehicles and buses</a>
</div>

<span id="7844" class="" style="width: 100%;">enter code here
<div class="e1EventDetails">
<span class="e1EventInfo">
<div class="e1EventDescription">
<div class="e1EventID">
<div class="e1ListingCount">
<div class="e1ClosingDate">
<div class="e1EventHighlights">
</span>
</div>
</span>
</span>
</li>

<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<span id="7846" class="" style="width: 100%;">
</span>
</li>

<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon ribbon-white">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/listing?listingid=310786">2009 International 9200i Truck Tractor</a>
</div>
<span class="e1ListingInfo">
<span id="310786" class="e1ListingOffer" data-lastupdated="1430388219706">
</span>
</li>

<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/listing?listingid=312207">2009 Mack CXU613 Vision Truck Tractor</a>
</div>
<span class="e1ListingInfo">
<span id="312207" class="e1ListingOffer" data-lastupdated="1430388219706">
</span>
</li>

</ul>

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容在页面上获取“a”标记WebElements的列表:

List<WebElement> aTagsList = driver.findElements(By.tagName("a"));

然后,您可以根据需要迭代aTagsList。

编辑: 要单击aTagsList中的第4个“a”标记WebElement,可以使用以下命令:

WebElement requiredElement = aTagsList.get(4);
requiredElement.click();

答案 1 :(得分:0)

它总是要按下的第4项吗?

如果没有,您必须知道要点击的内容。它取决于文本或“listingid”。

driver.findElements(By.cssSelector("a[href*='310786']"));

driver.findElement(By.cssSelector("a:(*'2009 International 9200i Truck Tractor'*)")); );