[英]Get rid of a pop up window while infinite scrolling using selenium
我想在此頁面上執行無限向下滾動: https://www.financialjuice.com/home
向下滾動一些后,出現 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)
我需要這個彈出窗口的解決方案,這樣我就可以在這個 web 頁面上無限向下滾動
實現一種方法來檢測彈出窗口是否存在並丟棄它。
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 端為此打開了一個錯誤。 一些信息在這里
如前所述,您可以暫時禁用它:
options = webdriver.ChromeOptions()
options.add_experimental_option('w3c', False)
driver = webdriver.Chrome(options=options)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.