简体   繁体   English

xPath适用于最后一页,但不适用于第一页-Selenium Java

[英]xPath is working for the last page but not the first - Selenium Java

I want to extract some data from NCBI using Selenium in Java 我想使用Java中的Selenium从NCBI提取一些数据

The problem is that the xPath /html/body/div[2]/div[1]/form/div[1]/div[5]/div/div[5]/div[" + i + "]/div[2]/p/a to get every article's title on the page and click on them, works for the last page but not for the first page and I couldn't understand why. 问题是xPath /html/body/div[2]/div[1]/form/div[1]/div[5]/div/div[5]/div[" + i + "]/div[2]/p/a可以在页面上获得每篇文章的标题并单击它们,它们适用于最后一页,但不适用于第一页,我不明白为什么。

driver.findElement(By.xpath("/html/body/div[2]/div[1]/form/div[1]/div[5]/div/div[5]/div[" + i + "]/div[2]/p/a")).click();

I suspect this is an off-by-one error as XPath's indexing starts with 1 and having div[0] there would not match anything. 我怀疑这是一个不正确的错误,因为XPath的索引从1开始,而div[0]在那里不匹配任何内容。

In general, though, your expression is very fragile as it is an absolute expression with the path starting with the very root of the HTML tree down to the desired element going through every parent. 但是,总的来说, 您的表达式非常脆弱,因为它是一个绝对表达式,其路径从HTML树的根部开始一直到每个父级都需要的元素。 Any minor layout change would break it. 任何较小的布局更改都会破坏它。

Instead, we could seriously simplify the expression: 相反,我们可以认真简化表达式:

//div[contains(@class, 'rslt')]/p/a

this would match all 20 links per page. 这将匹配每页所有20个链接。

Or, if you prefer more concise CSS selectors: 或者,如果您希望使用更简洁的CSS选择器:

driver.findElements(By.cssSelector(".rslt > p > a"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM