简体   繁体   English

分页和单击上的 Python Selenium 循环

[英]Python Selenium loop on Pagination and Click

I am trying to find a solution with Python and selenium to scrap the data of a website.我正在尝试使用 Python 和 selenium 找到解决方案来废弃网站的数据。 On the screenshot bellow the highest pagination is '40'.在下面的屏幕截图中,最高分页为“40”。 But next scrapping, it can be any value over 1.但是下一次报废,它可以是大于 1 的任何值。

在此处输入图片说明

在此处输入图片说明

# find Highest number in data-test-pagination-page-btn
s = # How can i get the highest pagination value with Selenium? 
print(s)
total_pages = int(s)

# How can I make python click on the pagination button 1,2,3 until S value?
for p in range (total_pages):
    driver.find_element_by_xpath("//ul//li[@xxxxxxxxxxxxxxxxxx']").click()
    time.sleep(3)

Is it possible to do this with selenium and python?是否可以使用 selenium 和 python 来做到这一点?

I am sure there is an easier solution that includes the attributes of the last element but since I don't know which website we're looking at, I am going to post the universal solution:我确信有一个更简单的解决方案,其中包含最后一个元素的属性,但由于我不知道我们正在查看哪个网站,我将发布通用解决方案:

parent = driver.find_element_by_id("ember334")
children = parent.find_elements_by_tag_name("li")  # note the plural

for child in children:
    last_page = child.get_attribute("data-test-pagination-btn")
s = last_page

Since it overwrites with every iteration the last_page , only the last children with the highest pagination counts.由于每次迭代都会覆盖last_page ,因此只有具有最高分页的最后一个子last_page才会计数。 A selenium object is not subscriptable, therefore you cant do s = children[-1].get_attribute("data-test-pagination-btn") selenium 对象不可下标,因此您不能执行s = children[-1].get_attribute("data-test-pagination-btn")

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

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