[英]Selenium FindElements method Java
In my project I use findElement(By.linkText("")).click();
在我的项目中,我使用findElement(By.linkText("")).click();
but in the web page I'm using, many links have the same name(btw I can use By.linktext only) so i want to know it its possible to navigate among the links which have the same name但是在我正在使用的 web 页面中,许多链接具有相同的名称(顺便说一句,我只能使用 By.linktext)所以我想知道它可以在具有相同名称的链接之间导航
Also when i use List<WebElement> h=driver.findElements(By.linkText("Years"));
另外,当我使用List<WebElement> h=driver.findElements(By.linkText("Years"));
and print it:并打印出来:
[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],
update: In the page im working on, every 2nd link text with the same name is where i would like to go to, also there isnt any difference in those links except their description eg: link 1: Years ABC(description)更新:在我正在处理的页面中,每个具有相同名称的第二个链接文本都是我想要 go 的地方,除了它们的描述之外,这些链接也没有任何区别,例如:链接 1:ABC 年(描述)
link 2: Years XYZ(description) can i use the description as a condition in any way??链接2:XYZ年(描述)我可以以任何方式使用描述作为条件吗?
If you observe the output:如果观察 output:
[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],[[[FirefoxDriver: firefox on LINUX (f5681889-d73a-4a0f-88e9-2e189491049b)] -> link text: Years],...]
All the elements are having the linkText as Years .所有元素的linkText 都为Years 。 So apperantly you can should be able to use:所以显然你应该能够使用:
List<WebElement> h=driver.findElements(By.linkText("Years"));
But, invoking click()
on certain elements at times changes the DOM Tree and the saved elements within the list may turn into stale elements .但是,有时在某些元素上调用click()
会更改DOM 树,并且列表中保存的元素可能会变成陈旧的元素。
In these cases, printing the link names may fail as well as assertions .在这些情况下, 打印链接名称和断言可能会失败。
Hence as per the best practices, you may like to invoke click()
on the specific linkTexts
as the necessity arises inducing WebDriverWait .因此,根据最佳实践,您可能希望在特定的linkTexts
上调用click()
,因为需要引入WebDriverWait 。 As an example:举个例子:
cssSelector
: cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_css"))).click();
xpath
: xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
so i couldn't find the solution to my original question, but as for the updated question i just iterated to the second element of the list i got using: List h=driver.findElements(By.linkText("Years"));所以我找不到原始问题的解决方案,但至于更新的问题,我只是迭代到我使用的列表的第二个元素: List h=driver.findElements(By.linkText("Years")); h.get(1).click(); h.get(1).click(); although it would be much efficient if i could get the destination URL of all the links generated by the search engine尽管如果我能获得搜索引擎生成的所有链接的目标 URL 会非常有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.