[英]How to click a link with Tag name <a class </a> using Java in Selenium WebDriver
I have below HTML code, where I have different <'li' class> tags under the <'ul'> tag. 我有下面的HTML代码,在<'ul'>标记下有不同的<'li'class>标记。 Within the <'li'> class tag, I will have tag which needs to be clicked. 在<'li'>类标记中,我将具有需要单击的标记。 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. 如果它在页面的3或4索引中存在,我需要单击该链接。 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>
You could get a List of the 'a' tag WebElements on the page using the following: 您可以使用以下方法在页面上获得'a'标签WebElement的列表:
List<WebElement> aTagsList = driver.findElements(By.tagName("a"));
You can then iterate over aTagsList as needed. 然后,您可以根据需要遍历aTagsList。
Edit: To click the 4th 'a' tag WebElement in aTagsList, you can use the following: 编辑:要单击aTagsList中的第四个'a'标签WebElement,可以使用以下命令:
WebElement requiredElement = aTagsList.get(4);
requiredElement.click();
Is it always the 4:th item you want to press? 它始终是您要按的第4个项目吗?
If not, you have to know what you want to click. 如果不是,则必须知道要单击的内容。 Either it is depending on the text or the "listingid". 它取决于文本还是“ listingid”。
driver.findElements(By.cssSelector("a[href*='310786']"));
or 要么
driver.findElement(By.cssSelector("a:(*'2009 International 9200i Truck Tractor'*)")); );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.