使用 Selenium 滚动弹出窗口时出现问题

问题描述 投票:0回答:1

我使用 Python 和 Selenium 进行网络抓取和自动化。我正在尝试登录 Instagram,转到特定帐户,单击他们的关注者,能够滚动弹出窗口并关注弹出窗口中可见的人。这是我的代码:

import time
from selenium.common.exceptions import ElementClickInterceptedException
import selenium as selenium
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
SIMILAR_ACCOUNT = "honeymoon"
USERNAME = "username"
PASSWORD = "password"

class InstaFollower:
    def __init__(self):
        
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option("detach", True)
        self.driver = webdriver.Chrome(options=chrome_options)
        self.driver.get("https://www.instagram.com")
    def login(self):
        self.driver.get("https://www.instagram.com")

         
        login_form = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="loginForm"]')))

         
        username = login_form.find_element(By.NAME, 'username')
        password = login_form.find_element(By.NAME, 'password')
        login_button = login_form.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button')

         
        username.send_keys(USERNAME)
        password.send_keys(PASSWORD)
        login_button.click()

         
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@aria-label="Instagram"]')))

    def find_followers(self):

        time.sleep(5)
         
        self.driver.get(f"https://www.instagram.com/{SIMILAR_ACCOUNT}/followers")

        time.sleep(5.2)
        
        modal_xpath = ('/html/body/div[6]/div[1]/div/div[2]/div/div/div/div/div[2]/div/div')
        modal = self.driver.find_element(by=By.XPATH, value=modal_xpath)
        for i in range(10):
             
             
            self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", modal)
            WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, modal_xpath)))


    def follow(self):
        all_buttons = self.driver.find_elements(By.XPATH, value='/html/body/div[6]/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[4]/div[1]/div/div[1]/div/div/div/div[3]/div/button/div/div')

        for button in all_buttons:
            try:
                button.click()
                time.sleep(1.1)
             
            except ElementClickInterceptedException:
                cancel_button = self.driver.find_element(by=By.XPATH, value="//button[contains(text(), 'Cancel')]")
                cancel_button.click()



bot = InstaFollower()
bot.login()
bot.find_followers()
bot.follow()

但我收到此错误:

Traceback (most recent call last):
  File "C:\Users\38161\PycharmProjects\ InstagramFollowerBot\main.py", line 78, in <module>
    bot.find_followers()
  File "C:\Users\38161\PycharmProjects\ InstagramFollowerBot\main.py", line 52, in find_followers
    modal = self.driver.find_element(by=By.XPATH, value=modal_xpath)
  File "C:\Users\38161\PycharmProjects\ InstagramFollowerBot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 742, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "C:\Users\38161\PycharmProjects\ InstagramFollowerBot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 348, in execute
    self.error_handler.check_response(response)
  File "C:\Users\38161\PycharmProjects\ InstagramFollowerBot\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[6]/div[1]/div/div[2]/div/div/div/div/div[2]/div/div"}
  (Session info: chrome=120.0.6099.111); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
    GetHandleVerifier [0x00007FF657C32142+3514994]
    (No symbol) [0x00007FF657850CE2]
    (No symbol) [0x00007FF6576F76AA]
    (No symbol) [0x00007FF657741860]
    (No symbol) [0x00007FF65774197C]
    (No symbol) [0x00007FF657784EE7]
    (No symbol) [0x00007FF65776602F]
    (No symbol) [0x00007FF6577828F6]
    (No symbol) [0x00007FF657765D93]
    (No symbol) [0x00007FF657734BDC]
    (No symbol) [0x00007FF657735C64]
    GetHandleVerifier [0x00007FF657C5E16B+3695259]
    GetHandleVerifier [0x00007FF657CB6737+4057191]
    GetHandleVerifier [0x00007FF657CAE4E3+4023827]
    GetHandleVerifier [0x00007FF6579804F9+689705]
    (No symbol) [0x00007FF65785C048]
    (No symbol) [0x00007FF657858044]
    (No symbol) [0x00007FF6578581C9]
    (No symbol) [0x00007FF6578488C4]
    BaseThreadInitThunk [0x00007FF896867344+20]
    RtlUserThreadStart [0x00007FF896E626B1+33]

我尝试了不同的 Xpath,但仍然不起作用。它登录,转到特定帐户并单击其关注者,但它不关注可见帐户,也不会滚动弹出窗口。

python selenium-webdriver
1个回答
0
投票

我遇到了类似的问题,问题是 Instagram XPATH 和选择器是动态的,因此仅使用 XPATH 总是会抛出错误,因为它不断变化。您有 2 个选择:

  1. 在 XPATH 中使用 contains() 和 text(): (例如:假设您需要一个显示“Click Me”的按钮)

button = driver.find_element(By.XPATH, value="//button[contains(text(), 'Click Me')]")

  1. 对元素使用乱码类名或 css 选择器(有时这很困难,但使用 chrome 上的检查工具并查看 css 元素对我有所帮助)

第一次发帖,所以对任何礼仪错误表示歉意,但最近正在为此奋斗,所以我希望这会有所帮助。 :)

© www.soinside.com 2019 - 2024. All rights reserved.