简体   繁体   English

在 Selenium webdriver 中单击一个链接后如何获得新出现的链接?

[英]How to get new appearing links after clicking one in Selenium webdriver?

I'm writing a test when you get all the links from the website and click them.当您从网站获取所有链接并单击它们时,我正在编写测试。 But I need to click some links that in the beginning are hidden or some links that appear only in other pages.但是我需要点击一些一开始隐藏的链接或一些只出现在其他页面中的链接。 Till now, I'm only got to the point that test gets all the active links from homepage and clicks them.到目前为止,我只知道测试从主页获取所有活动链接并单击它们。 I'm new to Selenium webdriver and java, so can you suggest how should I write the test that checks for new appearing links after clicking one or something similar?我是 Selenium webdriver 和 java 的新手,所以你能建议我应该如何编写在单击一个或类似的东西后检查新出现的链接的测试吗?

I usually write my Selenium tests much more specifically, but were I to attempt this I suspect I would start by making use of findElements(By.tagName("a")) to get all currently available anchors, probably put them into a data object which included if that anchor as been clicked yet and put those data objects into a Set.我通常更具体地编写我的 Selenium 测试,但是如果我尝试这样做,我怀疑我会首先使用 findElements(By.tagName("a")) 来获取所有当前可用的锚点,可能将它们放入一个数据对象中其中包括该锚点是否已被单击并将这些数据对象放入一个集合中。 Map that Set to the currentUrl to keep track of what links were found on what pages.将 Set 映射到 currentUrl 以跟踪在哪些页面上找到了哪些链接。 After a click (and recording that you clicked that anchor in its data object) you could check the currentUrl (without any #s) with the last one (without any #s) to determine if that click loaded a new page.单击(并记录您在其数据对象中单击该锚点)后,您可以检查 currentUrl(不带任何 #s)和最后一个(不带任何 #s)以确定该单击是否加载了新页面。 If the urls match, I would call findElements again and add those to the existing Set.如果 urls 匹配,我会再次调用 findElements 并将它们添加到现有的 Set 中。 If they don't match repeat the process for the new currentUrl.如果它们不匹配,则为新的 currentUrl 重复该过程。 Some additional things to be aware of would be handling new Windows and frames, which would require a switchTo and iterating through all the frames (and nested frames).需要注意的其他一些事情是处理新的 Windows 和框架,这需要 switchTo 并遍历所有框架(和嵌套框架)。

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

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