Selenium webdriver 单击分页页面并检查元素是否存在 [英] Selenium webdriver click through pages of a pagination and check if an element is present
问题描述
我试图点击网站上的分页页面和加载的每个页面上我需要检查是否存在在上一步中创建的元素,因为页面的分配是动态的,我以前不知道它创建了新创建的元素将显示在哪个页面上,因此需要检查所有内容直到找到它.
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.
谢谢
李
推荐答案
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 单击分页页面并检查元素是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!