Home > Enterprise >  How to fix AttributeError: 'NoneType' object has no attribute 'click' in insta b
How to fix AttributeError: 'NoneType' object has no attribute 'click' in insta b

Time:09-22

**after running the code bellow i am getting the

AttributeError: 'NoneType' object has no attribute 'click'.

pleas some one help me to fix this**

rest is the code:->

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager

import random

import time

from selenium.webdriver.common.by import By



def sleep_for_period_of_time():
    

     limit = random.randint(7,10)
    
     time.sleep(limit)

user = input("Enter your username: ")

pwd = input("Enter your password: ")

def main():
    
    options = webdriver.ChromeOptions()
    
    options.add_argument("--lang=en")
    
    browser = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
    
    browser.get("https://www.instagram.com")
    
    time.sleep(5)

    username_input = browser.find_element(By.CSS_SELECTOR,"input[name='username']")
    password_input = browser.find_element(By.CSS_SELECTOR,"input[name='password']")

    username_input.send_keys(user)
    password_input.send_keys(pwd)
    sleep_for_period_of_time()

    login_button = browser.find_element_by_xpath("//button[@type='submit']")
    login_button.click()
    sleep_for_period_of_time()

    not_now = browser.find_element_by_xpath("//div[@class='cmbtv']/button")
    not_now.click()
    sleep_for_period_of_time()

    page_ig = input("Enter page username: ")
    browser.get(f"https://www.instagram.com/{page_ig}")
    sleep_for_period_of_time()

    followers_link = browser.find_element_by_xpath("//ul/li[2]/a")
    followers_link.click()
    sleep_for_period_of_time()

    num_follow = input("How many person you want to follow: ")

    while(True):
        try:
            i = 0
            list_of_followers = browser.find_elements(By.XPATH, '//button/div/div[contains(text(), "Follow")]')
            for person in list_of_followers:
                if person.text == "Follow":
                    person.click()
                    print("Followed!")
                    i  =1
                    print(i)
                    sleep_for_period_of_time()
                else:
                    pass
                if i >= int(num_follow):
                    break

            browser.execute_script("arguments[0].scrollIntoView(true);", list_of_followers[i])

            answer = input("The programm finished! Click on 'e' to exit.. ")
            if answer.lower().startswith("e"):
                browser.quit()
                exit()

        except Exception as e:
            print(e)

if __name__ == "__main__":
    main()

Heading

complete error is:->

  D:\Users\singh\PycharmProjects\IG-Bot-main\fuf.py:21: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  browser = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
Traceback (most recent call last):
  File "D:\Users\singh\PycharmProjects\IG-Bot-main\fuf.py", line 77, in <module>
    main()
  File "D:\Users\singh\PycharmProjects\IG-Bot-main\fuf.py", line 33, in main
    login_button.click()
AttributeError: 'NoneType' object has no attribute 'click'

Process finished with exit code 1

CodePudding user response:

Your issue is that

login_button = browser.find_element_by_xpath("//button[@type='submit']")

Is not finding a button object. My guess is either your xpath find element is incorrect, or sleep_for_period_of_time() is not sleeping for long enough. You could try something like this

login_button = None
while not login_button:
    sleep_for_period_of_time()
    login_button = browser.find_element_by_xpath("//button[@type='submit']")

So that you will never try to click a button that has not yet been found.

CodePudding user response:

Error: AttributeError: 'NoneType' object has no attribute 'click'

This error can be caused by either the button type mentioned here is not found (OR) Due to missing allow cookies accept button click, before redirecting to login. (OR) the button might disabled

If the button is disabled, Try:

    login_button = browser.find_element("xpath","//button[@class='sqdOP  L3NKy   y3zKF     ']")

    # try removing the attribute.
    browser.execute_script('arguments[0].removeAttribute("disabled");', login_button)
    login_button.click()

So, After Calling the browser.get(), Find cookies accept button class and click on it, which can redirect to the login button.

If cookies are opened before clicking login:

    cookie = browser.find_element("xpath","//button[@class='aOOlW  bIiDR  ']") 
    cookie.click()

And as per selenium 4.3.0 , the method find_element_by_xpath has been depricated.

  • Related