我无法在Instagram上点击赞按钮/ Xpath

时间:2019-08-01 11:50:21

标签: python selenium xpath

我无法使用硒和XPath在Instagram中单击“赞”按钮。

您可以看到我已经在这里尝试过其他方法:I'm making a bot that likes every post that aren't liked yet

代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


class InstagramBot:

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Chrome("C:/Users/name/Downloads/chromedriver_win32 (1)/chromedriver.exe")

    def closeBrowser(self):
        self.driver.close()

    def login(self):
        driver = self.driver
        driver.get("https://www.instagram.com/")
        login_button = driver.find_element_by_xpath("//a[@href='/accounts/login/?source=auth_switcher']")
        login_button.click()
        time.sleep(1)
        user_name_elem = driver.find_element_by_xpath("//input[@name='username']")
        user_name_elem.clear()
        user_name_elem.send_keys(self.username)
        password_elem = driver.find_element_by_xpath("//input[@name='password']")
        password_elem.clear()
        password_elem.send_keys(self.password)
        password_elem.send_keys(Keys.RETURN)
        time.sleep(2)
        not_now_button = driver.find_element_by_xpath("//button[@class='aOOlW   HoLwm ']")
        not_now_button.click()
        time.sleep(1)

    def like_photo(self):
        driver = self.driver
        driver.get("https://www.instagram.com")
        time.sleep(1)
        for i in range(1, 4):
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(2)

        # find all the heart links
        hrefs = driver.find_elements_by_xpath("//button[@class='dCJp8 afkep _0mzm-']")
        pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
        pic_hrefs = [href for href in pic_hrefs]
        print(' Photos ' + str(len(pic_hrefs)))

        for _ in pic_hrefs:
            driver.get("https://www.instagram.com")
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            try:
                # list of WebElements
                like_buttons = driver.find_elements_by_xpath("//button[@class='dCJp8 afkep _0mzm-']")
                for button in like_buttons:
                    button.click()
                    time.sleep(18)
            except Exception as e:
                print(e)


sofusIG = InstagramBot("username", "password")
sofusIG.login()
sofusIG.like_photo()

错误输出: '消息:元素点击被拦截:元素...在点(69,20)不可点击。其他元素将获得点击:...   (会话信息:chrome = 75.0.3770.142)'

0 个答案:

没有答案