【问题标题】:How to click a link with Tag name <a class </a> using Java in Selenium WebDriver如何在 Selenium WebDriver 中使用 Java 单击标记名称 <a 类 </a> 的链接
【发布时间】:2017-09-29 02:45:19
【问题描述】:

我有以下 HTML 代码,其中 标签下有不同的 标签。在 类标签中,我将有需要点击的标签。这些是网页中一些搜索的结果,其中所有项目都将显示为带有分页的列表。

我需要验证这是否存在?如果它存在于页面上的第 3 或第 4 个索引中,我需要单击该链接。请让我知道如何将所有这些项目放入数组中并循环它们并单击随机索引中可用的任何产品。

<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>

【问题讨论】:

  • 元素元素 = driver.findElement(By.cssSelector("li.e1ListingItem a.jsListingURL")); if (element != null){}else{};
  • 谢谢degr,你能详细解释一下吗,我的场景是我搜索时返回了15个产品,现在我必须点击列表中的第4个产品。问题是我需要单击的链接和我不应该单击的链接都有“”jsListingURL e1ListingTitleLink”。但是,href 发生了变化……你能告诉我如何处理这个问题吗?谢谢!
  • 你有困难的 html 结构。我认为有足够的信息可供选择。您也可以将跨度与 id 标识符和“nth-child”选择器一起使用。阅读这篇文章code.tutsplus.com/tutorials/…

标签: java selenium


【解决方案1】:

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

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

然后您可以根据需要遍历 aTagsList。

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

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

【讨论】:

  • 谢谢汤姆。我能够单击该元素,但我仍然看到一些问题。是否可以在获取 aTagsLIst 时编写条件以检查标签“href="/listing?listingid"?谢谢。
【解决方案2】:

总是要按第 4 项吗?

如果没有,你必须知道你想点击什么。取决于文本或“listingid”。

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

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

【讨论】:

猜你喜欢
相关资源
最近更新 更多
热门标签