Неправильный синтаксис элемента Gram Bot Xpath - PullRequest
0 голосов
/ 15 февраля 2020

Я пытаюсь заставить кнопку «лайк» щелкнуть в самом сердце на странице Instagram и продолжить, мне удалось заставить ее работать без кнопки «лайк» только для того, чтобы оставлять комментарии, но мне бы очень хотелось, если бы она также могла щелкните по сердцу и перейдите к оставшейся части поста и хэштегам. Вот код ниже

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

class GaryVee:
    username = ''
    password = ''

    hashtags = [
        'wanderlust', 'travels',
    ]

    comments = [
        'Your posts are amazing', 'Amazing work. Keep going!', 'Your photos are magnificent',
        'Your work fascinates me!', 'I like how you put your posts together', 'Great job',
        'What a really nice photo, great job!', 'Well done!', 'Your posts are amazing',
    ]

    links = []

    price = 0.0

    def __init__(self):
        self.browser = webdriver.Firefox()
        self.login()
        self.hustle()

    def login(self):
        self.browser.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
        time.sleep(2)

        username_field = self.browser.find_element_by_xpath("//input[@name='username']")
        username_field.clear()
        username_field.send_keys(self.username)
        time.sleep(1)

        password_field = self.browser.find_element_by_xpath("//input[@name='password']")
        password_field.clear()
        password_field.send_keys(self.password)
        time.sleep(1)

        login_button = self.browser.find_element_by_xpath("//button[@type='submit']")
        login_button.click()
        time.sleep(2)

    def hustle(self):
        self.getTopPosts()
        self.execute()
        self.finalize()

    def getTopPosts(self):
        for hashtag in self.hashtags:
            self.browser.get('https://www.instagram.com/explore/tags/' + hashtag +'/')
            time.sleep(2)

            links = self.browser.find_elements_by_tag_name('a')
            condition = lambda link: '.com/p/' in link.get_attribute('href')
            valid_links = list(filter(condition, links))

            for i in range(0, 9):
                link = valid_links[i].get_attribute('href')
                if link not in self.links:
                    self.links.append(link)

    def execute(self):
        for link in self.links:
            self.browser.get(link)
            time.sleep(1)

            self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(1)

            self.comment()
            time.sleep(2)

            self.like()

            self.price += 0.02
            sleeptime = random.randint(18, 28)
            time.sleep(sleeptime)

    def comment(self):
        comment_input = lambda: self.browser.find_element_by_tag_name('textarea')
        comment_input().click()
        comment_input().clear()

        comment = random.choice(self.comments)
        for letter in comment:
            comment_input().send_keys(letter)
            delay = random.randint(1, 7) / 30
            time.sleep(delay)

        comment_input().send_keys(Keys.RETURN)

    def like(self):
# THIS IS THE LINE OF CODE IM HAVING TROUBLE WITH ----
        like_button = lambda: self.browser.find_element_by_xpath('//span[@aria-label="like"]').click()
        like_button().click()
# IN BETWEEN HERE ----

    def finalize(self):
        print 'You gave $' + string(self.price) + ' back to the community.'
        self.browser.close()
        sys.exit()

garyVee = GaryVee()

Это сообщение об ошибке, которое я получаю:

Traceback (most recent call last):
  File "test.py", line 106, in <module>
    garyVee = GaryVee()

  File "test.py", line 28, in __init__
    self.hustle()

  File "test.py", line 50, in hustle
    self.execute()

  File "test.py", line 78, in execute
    self.like()

  File "test.py", line 99, in like
    like_button().click()

  File "test.py", line 98, in <lambda>
    like_button = lambda: self.browser.find_element_by_xpath('//span[@aria-label="like"]').click()

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)

  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response

    raise exception_class(message, screen, stacktrace)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...