使用XPath单击超文本链接 [英] Clicking on a hypertext link using XPath

查看:138
本文介绍了使用XPath单击超文本链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个愚蠢的问题,如果这是一个蠢的问题 - 我是新到Selenium。



我有一个网页,我测试有一些超文本链接表。 HTML如下所示:

 < table> 
< thead>
< tr>
< td>< b>历史< / b>< / td>
< td>< b>属性< / b>< / td>
< td>< b> Xml< / b>< / td>
< / tr>
< / thead>
< tbody>
< tr>
< td>< a href = link here>显示< / a>< / td>
< td>< a href = link here>显示< / a>< / td>
< td>< a href = link here>显示< / a>< / td>
< / tr>
< / tbody>
< / table>

我想测试每个显示链接的点击。它们都有相同的文本,所以我不能通过linktext引用它们。我已经通过XPath引用它们,所以:

  driver.findElement(By.xpath id ='content'] / div / form / div / table [2] / thead / tr / td [1])getText()


正确返回'History'和

  driver.findElement(By.xpath // div [@ id ='content'] / div / form / div / table [2] / tbody / tr / td [1])getText()
pre>

正确返回'Show'。



所以我会认为:

  driver.findElement(By.xpath(// div [@ id ='content'] / div / form / div / table [2] / tr / td [1]))。click()



<



如果我这样做:

  driver.findElement(By.linkText(Show))。click()


$ b b

它会点击第一个显示链接,这是我的期望。



我也可以:

  driver.findElement(By.xpath(// div [@ id ='content'] / div / form / div / table [2] .1.0)) .click()

b

  driver.findElement(By.xpath((// a [contains(text(),'Show')])[ b $ b  

因此,所有这些工作:

  driver.findElement(By.linkText(Show))。click()
driver.findElement(By.xpath(// div [@ id ='内容'] / div / form / div / table [2] .1.0))。click()
driver.findElement(By.xpath( )])[1]))。click()

p>

  driver.findElement(By.xpath(// div [@ id ='content'] / div / form / div / table [2] / tbody / tr / td [1]。)为什么要点击()



解决方案

列表links = driver.findElements(By.tagName(a));



然后通过链接迭代,并使用for循环


点击每一个

Apologies if this is a dumb question - I'm new to Selenium.

I have a web page I'm testing that has a few hypertext links in a table. The HTML looks like this:

<table>
  <thead>
    <tr>
      <td><b>History</b></td>
      <td><b>Attributes</b></td>
      <td><b>Xml</b></td>
    </tr>
  </thead>
  <tbody>    
    <tr>
      <td><a href=link here>Show</a></td> 
      <td><a href=link here>Show</a></td>
      <td><a href=link here>Show</a></td> 
    </tr>
  </tbody>
</table>

I want to test a click on each of the 'Show' links. They all have the same text, so I can't reference them by linktext. I've been referencing them by XPath, so that:

driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/thead/tr/td[1]").getText()

correctly returns 'History' and

driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]").getText()

correct returns 'Show'.

So I would think that:

driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]")).click()

would click on the 'Show' link in the first column. But it doesn't - nothing happens.

If I do:

driver.findElement(By.linkText("Show")).click()

it clicks on the first 'Show' link, which is what I expect.

I can also do:

driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2].1.0")).click()

and that works, as does

driver.findElement(By.xpath("(//a[contains(text(),'Show')])[2]")).click()

So, to sum, all of these work:

driver.findElement(By.linkText("Show")).click()
driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2].1.0")).click()
driver.findElement(By.xpath("(//a[contains(text(),'Show')])[1]")).click()

but this doesn't:

driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]")).click()

Why?

解决方案

List links = driver.findElements(By.tagName("a"));

then iterate through links and click on each one of them using for loop

这篇关于使用XPath单击超文本链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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