如何在Selenium WebDriver中选择li里面的锚标签? [英] How to select the anchor tag which is inside the li in Selenium WebDriver?

查看:207
本文介绍了如何在Selenium WebDriver中选择li里面的锚标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码。我想点击锚标记< a> ,它位于< li> 内。我试图使用 By.tagName(// li / a)获取值。我没有得到任何数据。

Below is my code. I want to click the anchor tag <a>, which is inside the <li>. I have tried to get the value using By.tagName("//li/a"). I didn't get any data.

我的HTML中有许多带 class =dropdown-menu的列表。我应该能够选择一个列表来点击下拉值。

There are many lists with class="dropdown-menu" in my HTML. I should be able to select a single list from which to click the drop down values.

<ul class="dropdown-menu" style="position: static; margin-bottom: 5px; *width: 50px;border-style: none;min-width: 45px;box-shadow: 0 0px;text-align: center;" aria-labelledby="dropdownMenu" role="menu">
  <li><a class="dropdown-add" style="cursor: pointer;">1</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">2</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">3</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">4</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">5</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">6</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">7</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">8</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">9</a></li>
  <li><a class="dropdown-add" style="cursor: pointer;">10</a></li>
</ul>


推荐答案

很容易获得所有锚点的列表,只需使用CSS选择器

It's easy to get list of all anchors, just use CSS Selectors

List<WebElement> anchors = webDriver.findElement(By.cssSelector("ul li a"));

然后选择你要点击的任何锚,让我们说

Then choose whatever anchor you want to click at, let's say

anchors.findElement(By.linkText("10")).click();

这篇关于如何在Selenium WebDriver中选择li里面的锚标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆