java 使用 Selenium 进行分页导航,将项目添加到 HashMap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40889413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Navigating through pagination with Selenium adding items to a HashMap
提问by Nazrod12
I have a list of WebElements on a results page that I need to extract and add to a HashMap.
我在结果页面上有一个 WebElements 列表,我需要将其提取并添加到 HashMap 中。
There is pagination on the webpage that looks like this: 1, 2, 3, 4, 5, Next. These are all links and when Next is clicked on, it will then navigate to Page 2 and display 6 on the pagination, whilst removing 1, and so on.
网页上的分页如下所示:1, 2, 3, 4, 5, Next。这些都是链接,当单击 Next 时,它将导航到第 2 页并在分页上显示 6,同时删除 1,依此类推。
List<WebElement> pagination = driver.findElements(By.xpath("//div[@class='pagination-container']//a"));
int s = pagination.size();
for(int i=0;i<=s;i++){
this.getAuthors();
driver.get(Constants.url);
Thread.sleep(5000);
pagination = driver.findElements(By.xpath("//div[@class='pagination-container']//a"));
pagination.get(i).click();
Thread.sleep(5000);
}
The getAuthors() method goes through the necessary elements on the page and adds them to the HashMap. So it will loop through all of the pages in the pagination list until it is completed. It will go back to the Page 1 which is saved as Constants.url.
getAuthors() 方法遍历页面上的必要元素并将它们添加到 HashMap。所以它会遍历分页列表中的所有页面,直到完成为止。它将返回保存为 Constants.url 的第 1 页。
It gets to page 5 but then gets stuck, I am not sure how to code in the other examples of 6, 7, 8 and clicking the Next button each time to access them, adding them to pagination list.
它到达第 5 页,但随后卡住了,我不知道如何在 6、7、8 的其他示例中编写代码,并每次单击“下一步”按钮访问它们,将它们添加到分页列表中。
Note: The Thread.sleep methods are there to enable the page time to load all elements on the page.
注意: Thread.sleep 方法用于启用页面时间以加载页面上的所有元素。
Any ideas?
有任何想法吗?
回答by jam
You can do something like this, assuming that the link is an <a>
html element you need
你可以做这样的事情,假设链接是<a>
你需要的html 元素
1 Create a List of Web elements for example <a>
elements driver.findElements(By.tagName("a"))
1 为示例<a>
元素创建一个 Web 元素列表driver.findElements(By.tagName("a"))
2 Iterate List retrieved from the last step and check some meta information of the specific link that you need to click on, for example the title elements.get(i).getAttribute("title");
2 迭代上一步检索到的List,检查需要点击的特定链接的一些元信息,例如标题 elements.get(i).getAttribute("title");
3 Compare if the title is the one you need if (title.equals("Next Page")) {
3 比较标题是否是您需要的 if (title.equals("Next Page")) {
3 Send click signal
3 发送点击信号
Here is a code snippet
这是一个代码片段
new WebDriverWait(
driver, TIME_TO_WAIT).until(
ExpectedConditions.presenceOfElementLocated(
By.tagName("a")));
List<WebElement> elements = driver.findElements(By.tagName("a"));
for (int i = 0; i < elements.size(); i++) {
String title = elements.get(i).getAttribute("title");
if (title.equals("Next Page")) {
elements.get(i).click();
break;
}
}