【问题标题】:Get rid of a pop up window while infinite scrolling using selenium使用 selenium 无限滚动时摆脱弹出窗口
【发布时间】:2021-04-03 04:48:19
【问题描述】:

我想在这个页面上无限向下滚动:https://www.financialjuice.com/home

向下滚动后,会出现一个窗口(注册),我需要将其删除以便向下滚动完成,但我不能,我创建了以下代码:

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

import time

u = "https://www.financialjuice.com/home"
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe")
driver.get(url)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")    
driver.implicitly_wait(60) # seconds
    
print('start scrolling')
for i in range(5):
    driver.find_element_by_css_selector('html').send_keys(Keys.END)
    print("scroll", i)
    time.sleep(5)
# the pop up window will appear after the 4th scroll

# the below code to try to click on the page to remove it
el=driver.find_elements_by_xpath('//*[@id="aspnetForm"]/div[3]/header/nav/div[2]/div/div[1]/div/a[1]/img[2]')[0]

action = webdriver.common.action_chains.ActionChains(driver)
# action.move_to_element_with_offset(el, 5, 5)
action.move_to_element_with_offset(el, 5, 0)

action.click()
action.perform()

### after remove it, I complete the scrolls:
for i in range(100):
    driver.find_element_by_css_selector('html').send_keys(Keys.END)
    print("scroll", i)
    time.sleep(1)

我需要解决此弹出窗口,以便我可以在此网页上无限向下滚动

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    实现一种方法来检测弹出框是否存在并将其丢弃。

    def ignore_sign_up_popover():
        sign_up = driver.find_elements_by_css_selector('#signup')
        if len(sign_up) > 0 and sign_up[0].is_displayed():
            TouchActions(driver).tap_and_hold(0, 0).release(0, 0).perform()
    

    然后在每次迭代时添加检查(之前的滚动)

    for i in range(5):
        ignore_sign_up_popover()
        driver.find_element_by_css_selector('html').send_keys(Keys.END)
        print("scroll", i)
        time.sleep(5)
    

    请注意,您需要导入 touchActions:

    from selenium.webdriver.common.touch_actions import TouchActions
    

    还有一个 w3c/chrome 的问题,它会引发 touchAction 异常,如果我没记错的话,在 Chrome 端打开了一个错误。一些信息here

    如前所述,您可以暂时禁用它:

    options = webdriver.ChromeOptions()
    options.add_experimental_option('w3c', False)
    driver = webdriver.Chrome(options=options)
    

    【讨论】:

    • 它适用于向下滚动 4 次后出现的第一个弹出窗口,但是,它不会删除向下滚动 +/- 50 次后出现的下一个弹出窗口
    • 对我来说它有效。您是否考虑使用driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") 而不是driver.find_element_by_css_selector('html').send_keys(Keys.END)。您也可以在不验证弹出框是否存在的情况下调用TouchActions(driver).tap_and_hold(0, 0).release(0, 0).perform()。它确实会在每次迭代时单击位置 (0, 0),但无论如何它们都不应该在该位置单击任何东西..
    猜你喜欢
    • 2020-05-10
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多