samedi 4 juillet 2020

Not able to reply instagram comments using selenium

I am writing a script to automate replies on Instagram to anyone who has 'pen' in their comment.

The script has to scan every post and reply to those who have 'pen' keyword in their comments. But whenever I run the Script, I faced a problem where the script only scans the first post and it's comments and scanning the next post. I have attached the script that I used.

href_found = driver.find_elements_by_tag_name("a")
pic_href = [ele.get_attribute('href') for ele in href_found if '.com/p' in ele.get_attribute('href')]
for ele in pic_href:
    driver.get(ele)
    time.sleep(3)

    if driver.find_element_by_xpath(("//div[contains(.,'pen')]"))
        replybox = driver.find_element_by_xpath("(//button[@class='FH9sR'][contains(.,'Reply')])[2]")
        replybox.click()
        time.sleep(5)
        commentbox = lambda: driver.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[2]/section[3]/div/form/textarea")
        commentbox().click()
        for i in comment:
            commentbox().send_keys(i)
            time.sleep(random.randint(1,7)/30)
        commentbox().send_keys(Keys.ENTER)
    else:
        continue

The error that I am facing is:

Traceback (most recent call last):
  File "./reply.py", line 55, in <module>
    reply("<username>",comment)
  File "./reply.py", line 37, in reply
    if driver.find_element_by_xpath(("//div[contains(.,'pen')]")):
  File "/home/mayank/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/home/mayank/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/home/mayank/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/mayank/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //div[contains(.,'pen')]

Aucun commentaire:

Enregistrer un commentaire