Clicking on a hypertext link using XPath

爱⌒轻易说出口 提交于 2019-12-01 23:30:01

You can gather all the links together by using findElements and then iterate through the array.

Example - List<WebElement> inputs = driver.findElements(By.xpath("//input"));

and in your case

List<WebElement> links = driver.findElements(By.linkText("Show"));

and then iterate through links and click on each one of them

Notice that the expression in question

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

selects a td element, in case of which no link or event is defined. While clicking it in browser should open a link, it will happen only because you're effectively clicking what's inside the tag as well as the td itself. The code above only clicks the td, ignoring its content.

You have to go one step deeper, to the a element within your currently selected td. Like this:

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!