简体   繁体   English

如何<a class </a>在Selenium WebDriver中使用Java</a>单击带有标签名称的链接

[英]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.

相关问题 如何使用Selenium Webdriver 2.25-Java单击ahref链接? - How to click on the ahref link using selenium webdriver 2.25 - java? 如何使用Selenium WebDriver(Java)单击图像/链接 - How to click on an image/link using Selenium WebDriver (Java) 如何使用Java中的Selenium WebDriver单击列表中的链接 - How to click the link located in a list using Selenium WebDriver in Java selenium webdriver单击标记中的链接 - selenium webdriver click link in the tag 如何使用 selenium webDriver 单击锚标记 - How to click on a anchor Tag using selenium webDriver 使用带有Java的Selenium WebDriver无法单击鼠标悬停链接 - Unable to click a mouse hover link using Selenium WebDriver with Java 无法使用Selenium Webdriver(Java)单击“确认电子邮件”链接 - Not able to click on “Confirm Email” link using selenium webdriver (Java) 如何通过Selenium WebDriver Java单击覆盖标记下的Webelement - how to click webelement under overlay tag through selenium webdriver java 在Java w / Selenium WebDriver中,如何仅在广告展示时如何使用JavaScript单击覆盖广告上的链接? - In Java w/Selenium WebDriver, how to I click link on overlay ad using JavaScript only when ad shows? 如何使用 Selenium Webdriver 和 Java 在框架内单击 iframe 内的链接 - How to click on a link inside an iframe which is inside a frame using Selenium Webdriver and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM