我正在从事 Instagram autosub 项目。这是一个动态网站,所以我需要向下滚动。我知道如何在主窗口中执行此操作,但由于“兴趣”是一个弹出窗口,因此这种滚动解决方案不起作用。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
chrome_option = webdriver.ChromeOptions()
chrome_option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_option)
driver.get(url="https://www.instagram.com/accounts/login")
USER = "USER"
EMAIL = ("EMAIL")
PASSWORD = "PASSWORD"
try:
cookies = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Allow all cookies')]"))
)
cookies.click()
enter_email = driver.find_element(By.XPATH, value='//input[@name="username"]')
enter_email.send_keys(EMAIL)
enter_password = driver.find_element(By.XPATH, value='//input[@name="password"]')
enter_password.send_keys(PASSWORD)
enter_password.send_keys(Keys.ENTER)
not_now = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "div ._ac8f div"))
)
not_now.click()
not_now2 = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]'))
)
not_now2.click()
driver.get(url=f"https://www.instagram.com/{USER}/")
follows = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/following')]"))
)
follows.click()
fBody = driver.find_element(By.XPATH, "//html/body/div[6]/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[4]']")
scroll = 0
while scroll < 5:
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
time.sleep(2)
scroll += 1
except Exception as e:
print(e)
我查看了 stachoverflow,询问了 GPT 聊天,看了一堆视频,但没有任何帮助。
Selenium Python:如何在弹出窗口中向下滚动 为什么模式/弹出窗口上的滚动在 Instagram 上不起作用 即使这些也没有帮助。
这是否只是这个版本的 Instagram 的问题?
Instagram 是一个无限源,您可以使用滚动来呈现您需要的所有信息。等待加载,然后获取您需要的信息。
喜欢:
while scroll<5:
element=""
try:
time.sleep(5)
element=drive.find_element(By.CLASS_NAME,"fbody")
except: # The exception you want to put
print("Not find")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);",fbody)
else:
if(element="")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);",fbody)
scroll+=1
else:
break