使用Python + Selenium在while循环中点击下一页 [英] Using Python + Selenium to click on next page with while loop

查看:9134
本文介绍了使用Python + Selenium在while循环中点击下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个名为iens的网站上浏览不同的网页。我使用selenium + python点击volgende(这意味着下一个在荷兰语),但我想让我的程序继续点击下一个,直到没有更多的页面使用while循环。所以在这种情况下,我想让我的程序在第23页结束。现在我可以进入第2页,通过关闭cookie弹出消息,等待,直到它关闭en然后点击Volgende按钮。 p>

我的代码如下所示:

 来自selenium.webdriver.support。 ui import WebDriverWait 
来自selenium import webdriver
来自selenium.webdriver.common.by import作者:
来自selenium.webdriver.support import expected_conditions as EC

chrome_path =' / Users / user / Downloads / chromedriver'
driver = webdriver.Chrome(chrome_path)
driver.get('https://www.iens.nl/restaurant+utrecht')

#wait的Cookie消息
close_icon = WebDriverWait(driver,5,
0.25).until(EC.visibility_of_element_located([By.CSS_SELECTOR,
'.cookiePolicy-close']) )

close_icon.click()

#wait cookie消息消失
WebDriverWait(driver,5,
0.25).until(EC。 invisibility_of_element_located([By.CSS_SELECTOR,
'.cookiePolicy-close']))

click_icon = WebDriverWait(driver,5,
0.25).until(EC.visibility_of_element_located([ By.LINK_TEXT,
'Volgende']))
click_icon.click()

该网站名为 https://www.iens.nl/restaurant+utrecht



提前感谢!

解决方案

切换到下一页, webdriver 尝试单击下一步按钮,但某些其他元素会收到点击。您可以使用此解决方案:

  while True:
try:
click_icon = WebDriverWait ,0.25).until(EC.visibility_of_element_located([By.LINK_TEXT,'Volgende'])
click_icon.click()
WebDriverWait(driver,5).until(EC.presence_of_element_located( CSS_SELECTOR,'main:not([style * =margin-top])')))
除了:
break

另一个简单的解决方案(只有当你的目标是到达最后一页)是获得最后一页,而不是点击下一步

  driver.find_elements_by_xpath('// div [@ class =pagination] / ul / li / a' -2] .click()


I'm trying to move through different pages on a website called iens. I'm using selenium + python to click on "volgende" (which means "next" in dutch), but I want my program to keep on clicking on next until there are no more pages left using a while loop. So in this case I want my program to end at page 23. Right now I'm able to get to page 2, by closing the cookie pop up message, waiting until it's closed en then clicking on the "Volgende" button.

My code looks like this:

 from selenium.webdriver.support.ui import WebDriverWait
 from selenium import webdriver
 from selenium.webdriver.common.by import By
 from selenium.webdriver.support import expected_conditions as EC

 chrome_path = '/Users/user/Downloads/chromedriver'
 driver = webdriver.Chrome(chrome_path)
 driver.get('https://www.iens.nl/restaurant+utrecht')

 #wait for cookie message
 close_icon = WebDriverWait(driver, 5, 
 0.25).until(EC.visibility_of_element_located([By.CSS_SELECTOR, 
 '.cookiePolicy-close']))

 close_icon.click()

 #wait for cookie message to disappear
 WebDriverWait(driver, 5, 
 0.25).until(EC.invisibility_of_element_located([By.CSS_SELECTOR, 
 '.cookiePolicy-close']))

 click_icon = WebDriverWait(driver, 5, 
 0.25).until(EC.visibility_of_element_located([By.LINK_TEXT, 
 'Volgende']))
 click_icon.click()

The website is called https://www.iens.nl/restaurant+utrecht

Thanks in advance!

解决方案

Because of automatic scrolling each time you switch to next page, webdriver tries to click Next button, but some other element receives click. You can use this solution:

while True:
    try:
        click_icon = WebDriverWait(driver, 5, 0.25).until(EC.visibility_of_element_located([By.LINK_TEXT, 'Volgende']))
        click_icon.click()
        WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'main:not([style*="margin-top"])')))
    except:
        break

Another simple solution (only if your goal is to reach last page) is to get last page without clicking Next all the time:

driver.find_elements_by_xpath('//div[@class="pagination"]/ul/li/a')[-2].click()

这篇关于使用Python + Selenium在while循环中点击下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆