我正在尝试浏览chefsteps帐户(这个帐户起作用了),然后点击我instagram帐户中的chefsteps帐户(点击部分不起作用)。但是我得到了“元素点击截获的异常”。没有可见的对话框,但'chefsteps‘按钮(元素单击)被截获。我应该怎么做才能解决这个问题?
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\Day52_instagram_followers_bot\main.py", line 65, in <module>
bot.find_followers()
File "C:\Users\DELL\PycharmProjects\Day52_instagram_followers_bot\main.py", line 38, in find_followers
chefsteps.click()
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="_3SOD">...</div> is not clickable at point (207, 135). Other element would receive the click: <div class="jLwSh" role="dialog"></div>
(Session info: chrome=94.0.4606.81)
import selenium.common.exceptions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
from time import sleep
CHROME_DRIVER_PATH ="C:\Development\chromedriver_win32\chromedriver.exe"
SIMILAR_ACCOUNT = "chefsteps"
INSTAGRAM_EMAIL = os.environ['YOUR_INSTAGRAM_EMAIL']
INSTAGRAM_PASSWORD = os.environ['YOUR_INSTAGRAM_PASSWORD']
class InstaFollower:
def __init__(self):
self.driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
def login(self):
self.driver.get('https://www.instagram.com/')
sleep(3)
email = self.driver.find_element_by_name('username')
email.send_keys(INSTAGRAM_EMAIL)
password = self.driver.find_element_by_name('password')
password.send_keys(INSTAGRAM_PASSWORD)
password.send_keys(Keys.ENTER)
pass
def find_followers(self):
sleep(3)
notif = self.driver.find_element_by_xpath('/html/body/div[5]/div/div/div/div[3]/button[2]')
notif.click()
browser = self.driver.find_element_by_class_name("x3qfX")
browser.send_keys(SIMILAR_ACCOUNT)
sleep(7)
try:
chefsteps = self.driver.find_element_by_class_name('uL8Hv')
chefsteps.click()
except selenium.common.exceptions.ElementClickInterceptedException:
chefsteps = self.driver.find_element_by_css_selector('._4EzTm div')
chefsteps.click()
def follow(self):
sleep(7)
followers_button = self.driver.find_element_by_class_name('-nal3')
followers_button.click()
sleep(5)
number_of_followers = int(followers_button.get_attribute("title").replace(",", ""))
print(number_of_followers)
n = 1
while n < number_of_followers:
try:
follow_buttons = self.driver.find_elements_by_class_name("y3zKF")
for i in follow_buttons:
sleep(1)
i.click()
except selenium.common.exceptions.ElementClickInterceptedException:
cancel = self.driver.find_element_by_class_name("HoLwm")
cancel.click()
continue
bot = InstaFollower()
bot.login()
bot.find_followers()
bot.follow()发布于 2021-10-09 15:41:24
尝试如下所示:
1:不确定按钮出现的位置,元素是否在滚动后出现
chefsteps = self.driver.find_element_by_class_name('uL8Hv')
self.driver.execute_script("arguments[0].scrollIntoView(true);",chefsteps)
chefsteps.click()2:可以使用ActionsChains
from selenium.webdriver import ActionChains
chefsteps = self.driver.find_element_by_class_name('uL8Hv')
actions = ActionChains(self.driver)
actions.move_to_element(chefsteps).click().perform()3:使用Javascript:
chefsteps = self.driver.find_element_by_class_name('uL8Hv')
self.driver.execute_script("arguments[0].click();",chefsteps)如果上述方法都不起作用,请检查元素是否在Iframe或shadow-root中。还要检查您正在使用的定位器,它在DOM中应该是唯一的,即1/1。
https://stackoverflow.com/questions/69507966
复制相似问题