为了账号安全,请及时绑定邮箱和手机立即绑定

硒蟒蛇 - 执行操作,单击下一页,重复直到最后一页

硒蟒蛇 - 执行操作,单击下一页,重复直到最后一页

哈士奇WWW 2022-09-13 19:37:53
我正在尝试对网页执行操作,单击“下一步”按钮,然后重复该操作,直到到达最后一页。我尝试过在堆栈溢出上使用类似问题的答案,但我无法让它们正常工作。现在唯一发生的事情是网页打开。我的代码都没有发生, 用网页做事.我的代码在下面。感谢您的帮助!从硒导入网络驱动程序从 webdriver_manager.chrome 导入 Chrome驱动程序管理器driver = webdriver.Chrome(ChromeDriverManager().install())driver.get('https://obamawhitehouse.archives.gov/briefing-room/speeches-and-remarks')while True:    next_page_btn = driver.find_elements_by_xpath("//li[@class = 'pagination-next']/a")    if len(next_page_btn) < 1:        print("No more pages left")        break    else:        <MY CODE>        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Next'))).click() 
查看完整描述

3 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

我注意到我的网站的页面是这样描述的:

https://obamawhitehouse.archives.gov/briefing-room/speeches-and-remarks?term_node_tid_depth=31&page=1

转到 。因此,我能够将我的代码包装在一个 while 循环中,添加一个计数器,然后执行 .page=473page={}.format


查看完整回答
反对 回复 2022-09-13
?
烙印99

TA贡献1829条经验 获得超13个赞

请检查以下解决方案供您参考:


from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.ui import WebDriverWait as Wait

from selenium.webdriver.support.ui import WebDriverWait

from selenium.common.exceptions import TimeoutException



driver = webdriver.Chrome(executable_path=r"\chromedriver.exe")


driver.get('https://obamawhitehouse.archives.gov/briefing-room/speeches-and-remarks')

wait = WebDriverWait(driver,30)


flag = True


while flag:

 try:

    element = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Next')]")))

    if (element != 0):

        element.click()


 except TimeoutException as ex:

        print "It is all good, no element there"


查看完整回答
反对 回复 2022-09-13
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

我看了一下这个网站,似乎分页的下一个类并不存在。相反,您要查找的“下一步”按钮具有类寻呼机 - 下一个最后一个

我建议然后改变这一点:

next_page_btn = driver.find_elements_by_xpath("*//li[@class = 'pagination-next']/a")

为此:

next_page_btn = driver.find_elements_by_xpath("*//li[@class = 'pager-next last']/a")

如果这有帮助,请告诉我!


查看完整回答
反对 回复 2022-09-13
  • 3 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号