首页 > 解决方案 > 如果它们都共享同一个 Div 类,我如何使用 Selenium 单击第三个 Div 类“链接”?

问题描述

我在 python 中使用 selenium。所有的 HTML 共享相同的 div 类“链接”。页面上共有七个链接,每个链接都具有相同的类和相同的名称。我想知道如果它们具有完全相同的名称 <div class"link"> 如何单击 web 元素

Selenium 与 python 使用 chrome webdriver。

<div class="aux">
  <div class="links">
     <a class="view" href="/pmc/articles/PMC1403861/">Summary</a>
     <a class="view" href="/pmc/articles/PMC1403861/?report=classic&amp;page=1">Page Browse</a>
     <a class="view" href="/pmc/articles/PMC1403861/pdf/jeabehav00206-0003.pdf">PDF–1.6M</a>
     <a class="view citationexporter" href="#" data-citationid="PMC1403861" role="button" aria-expanded="false" aria-haspopup="true">Citation</a>
  </div>
</div>

标签: htmlpython-3.xseleniumclick

解决方案


尝试使用以下语法 -

Python:

 driver.find_element_by_xpath("//div[@class = 'links']/a[3]")).click();

爪哇:

 driver.findElement(By.xpath("//div[@class = 'links']/a[3]")).click();

推荐阅读