【问题标题】:Have issue in clicking the items in the Instagram page using selenium使用 selenium 单击 Instagram 页面中的项目时遇到问题
【发布时间】:2021-12-17 23:28:09
【问题描述】:

您好,我在 Mac 上使用 Firefox。我尝试在 python 上使用 selenium 来单击标签中前 3 张图片的点赞按钮。但是,代码一直在检查点赞按钮是否被点击时出错,并且点赞按钮被其他项目挡住了。

我有错误

  1. ElementClickInterceptedException:元素在点 (311,460) 处不可点击,因为另一个元素遮住了它
  2. AttributeError: 'WebDriver' 对象没有属性 'findElement'

下面是代码。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
import time

sleeptime = 10

#login function for account etc
def login(browser):
    browser.maximize_window()
    browser.get("https://www.instagram.com")
    
    time.sleep(sleeptime)
    
    username = browser.find_element(By.NAME,"username")
    password = browser.find_element(By.NAME,"password")
    
    #grab all the button (take care if button change position, currently login is at the first) [0] is the first button
    login = browser.find_element(locate_with(By.TAG_NAME, "button").below(password))
    
    #input the username and password
    username.send_keys("Hkk_james38")
    password.send_keys("myfmox-tidgab-xotNo0")
    login.click()
    
    
    
    #to hold the browser to prevent it close itselfs
    time.sleep(sleeptime)

def Visit_Tag(driver, url):
    sleeptime = 6
    driver.get(url)
    time.sleep(sleeptime)
    
    #select all the pictures and save in the list
    pictures = driver.find_elements_by_css_selector("div[class = '_9AhH0']")
    print(pictures)
    image_count = 0 
    
    #click the like in each picture by loop
    for picture in pictures:
        
        if image_count > 3:
            break
                
        picture.click()
        time.sleep(sleeptime)
        
        #click the like button, cehck if the like button is click or not
        if driver.find_elements(By.XPATH,"//article//section//button//*[@aria-label='Unlike']").size() > 0:
            like = driver.find_element(By.XPATH,"//article//section//button//*[@aria-label='Like']")
        like.click()
       
        #close the tag
        close = driver.find_element(By.XPATH,"//article//section//button//*[@aria-label='Close']")
        close.click()
        
        image_count += 1
        
    
    
#main function to execute
def main():
    #find webdriver location for firefox
    service = Service('/usr/local/bin/geckodriver')
    browser = webdriver.Firefox(service = service)
    login(browser)
    
    #tag for the pages u will follow and interested in
    tags = {
        "programming",
        
        }
    
    websites = []
    
    for tag in tags:
        Visit_Tag(browser, f"https://www.instagram.com/explore/tags/{tag}")
        
    
main()

【问题讨论】:

    标签: python-3.x selenium-webdriver selenium-firefoxdriver


    【解决方案1】:

    正如您看到的错误消息中明确提到的那样,WebDriver 对象不存在此类方法或属性findElements
    而不是

    if driver.findElements(By.XPATH("//article//section//button//*[@aria-label='Unlike']")).size() > 0:
    

    尝试使用

    if driver.find_elements(By.XPATH("//article//section//button//*[@aria-label='Unlike']")).size() > 0:
    

    关于

    ElementClickInterceptedException:元素在点 (311,460) 处不可点击,因为另一个元素遮住了它

    错误:尚不清楚哪些代码行会导致该错误。
    您没有提供整个错误回溯,也没有提供网页 URL,所以我们无法猜测。

    【讨论】:

    • 对不起。我已经更新了我的完整代码。
    • 您的完整代码包含登录名和密码。
    • 你看过这个话题吗? stackoverflow.com/a/48667924/7427923
    • @HarryHau 感谢您粘贴代码。但目前还不清楚是什么代码行导致ElementClickInterceptedException
    • @DenisRomaniuk 我看到了这个话题。这绝对没有提供足够的信息是什么代码行导致ElementClickInterceptedException。现在也不清楚。至于凭证 - 它们肯定是一些测试帐户,而不是 OP 的真实帐户。
    猜你喜欢
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2022-10-05
    相关资源
    最近更新 更多