Selenium webdriver 单击分页页面并检查元素是否存在

编程入门 行业动态 更新时间:2024-10-12 03:21:58
本文介绍了Selenium webdriver 单击分页页面并检查元素是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图点击网站上的分页页面和加载的每个页面上我需要检查是否存在在上一步中创建的元素,因为页面的分配是动态的,我以前不知道它创建了新创建的元素将显示在哪个页面上,因此需要检查所有内容直到找到它.

I am trying to click through paginated pages on a website and on each page that loads I need to check if an element that was created in a previous step is present, as the assignment of pages is dynamic I don't know before its created which page the newly created element will be displayed on, so need to check all until I find it.

分页代码为

<div class="pagination" style="display: block; visibility: visible;"><ol><li class="current first"><a href="#">1</a></li><li><a href="#">2</a></li><li class="last"><a href="#">3</a></li></ol></div>

也有可能分页超过 3 页,我意识到我需要一个 for 或 while 循环,但是当我尝试读取存在的元素数量时出现错误.

Its also possible that there will be more than 3 pages in the pagination, I realize that I need a for or while loop, but I am getting errors when I try and read the number of elements that are present.

谢谢

推荐答案

使用 find_elements_by_xpath():

for page_link in driver.find_elements_by_xpath('//div[@class="pagination"]/ol/li/a'): print "page number: %s" % page_link.text page_link.click() # parse the page and find the element

另一种可能的解决方案是获取最大页码并迭代整数范围直到该最大值:

Another possible solution would be to get the maximum page number and iterate over the range of integers till that maximum:

max_page_element = driver.find_element_by_xpath('//div[@class="pagination"]/ol/li[@class="last"]/a') max_page = int(max_page_element.text) for page in xrange(1, max_page + 1): print "page number: %s" % page page_link = driver.find_element_by_xpath('//div[@class="pagination"]/ol/li/a[text()="%s"]' % page) page_link.click() # parse the page and find the element

更多推荐

Selenium webdriver 单击分页页面并检查元素是否存在

本文发布于:2023-11-27 16:06:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   单击   是否存在   元素   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!