简体   繁体   English

使用 selenium 无限滚动时摆脱弹出窗口 window

[英]Get rid of a pop up window while infinite scrolling using selenium

I want to perform infinite scroll down on this page: https://www.financialjuice.com/home我想在此页面上执行无限向下滚动: https://www.financialjuice.com/home

after some scrolls down, a window appear (sign up), and I need to remove it so the scroll down comlete, but I can't, I created this code:向下滚动一些后,出现 window(注册),我需要将其删除以便向下滚动完成,但我不能,我创建了以下代码:

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)

I need solution to this pop up so I can scroll down infinitely on this web page我需要这个弹出窗口的解决方案,这样我就可以在这个 web 页面上无限向下滚动

Implement a method to detect if the popover is present and discard it.实现一种方法来检测弹出窗口是否存在并丢弃它。

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()

Then add the check on each iteration (prior scrolling)然后在每次迭代中添加检查(滚动前)

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)

Note you will need to import touchActions:请注意,您需要导入 touchActions:

from selenium.webdriver.common.touch_actions import TouchActions

Also there is an issue with w3c/chrome which will raise an exception with the touchAction, if I remember correctly there is a bug opened on Chrome side for it. w3c/chrome 也有一个问题,它会引发 touchAction 异常,如果我没记错的话,Chrome 端为此打开了一个错误。 Some info here一些信息在这里

As mentioned, you can disable it for the time being with:如前所述,您可以暂时禁用它:

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

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

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