如何解决在Instagram帖子上输入评论的问题

时间:2019-06-11 05:21:01

标签: python selenium xpath

我只是编程的初学者。实际上,我尝试在Instagram上自动发表评论。但是,如果我通过chrome / firefox运行以下代码。它将包含差异错误

import time
import random
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class Commenter:

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Chrome()

    """closing browser"""
    def closeBrowser(self):
        self.driver.close()

    """login in to Instagram"""
    def login(self):
        driver = self.driver
        driver.get("https://www.instagram.com/")
        time.sleep(2)
        login_button = driver.find_element_by_xpath("//a[@href='/accounts/login/?source=auth_switcher']")
        login_button.click()
        time.sleep(2)
        user_name_elem = driver.find_element_by_xpath("//input[@name='username']")
        user_name_elem.clear()
        user_name_elem.send_keys(self.username)
        passworword_elem = driver.find_element_by_xpath("//input[@name='password']")
        passworword_elem.clear()
        passworword_elem.send_keys(self.password)
        passworword_elem.send_keys(Keys.RETURN)
        time.sleep(2)


    """write comment in text area using lambda function"""
    def write_comment(self, comment_text):
        try:
            comment_button = lambda: self.driver.find_element_by_link_text('Comment')
            comment_button().click()
        except NoSuchElementException:
            pass
##Add a comment…
        try:
            comment_box_elem = lambda: self.driver.find_element_by_xpath("//textarea[@aria-label='留言⋯⋯']")
            comment_box_elem().send_keys('')
            comment_box_elem().clear()
            for letter in comment_text:
                comment_box_elem().send_keys(letter)
                time.sleep((random.randint(1, 7) / 30))

            return comment_box_elem

        except StaleElementReferenceException and NoSuchElementException as e:
            print(e)
            return False

`com = Commenter(username='your login', password='your pw')
com.login()
time.sleep(3)
com.driver.get("https://www.instagram.com/p/ByhuUY3nmEJ/")
time.sleep(5)
com.write_comment("123123123123123dd")`

希望我可以通过Firefox / chrome对帖子发表评论。

当我使用Firefox时,会出现“无法通过键盘Instagram到达元素”

当我使用Chrome时,它将出现“ selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互   (会议信息:chrome = 75.0.3770.80)   (驱动程序信息:chromedriver = 74.0.3729.6(255758eccf3d244491b8a1317aa76e1ce10d57e9-refs / branch-heads / 3729 @ {#29}),platform = Windows NT 6.1.7601 SP1 x86_64)“

非常感谢您可以提出解决方案来解决我的问题。我已经搜索了相关的问题,但是我仍然不知道要解决此问题。谢谢您的帮助。

0 个答案:

没有答案