Keys.BACK_SPACE or Keys.TAB send key is not working on facebook selenium python

Viewed 751

I am writing some selenium code to navigate Facebook.

import os
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys


def driver():
    global driver
    driver = webdriver.Chrome("chromedriver.exe", chrome_options=chrome_options)
    driver.get("https://facebook.com")


def chrome_options():
    global chrome_options
    chrome_options = Options()
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument('--profile-directory=Default')
    # chrome_options.add_argument("--user-data-dir=chrome-data")
    prefs = {"profile.default_content_setting_values.notifications": 2}
    chrome_options.add_experimental_option("prefs", prefs)
    chrome_options.add_argument('disable-infobars')
    chrome_options.add_experimental_option("useAutomationExtension", False)
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])


def actions():
    global actions
    actions = ActionChains(driver)


def login():
    try:
        # I use environment veriable  base on this tutorials https://www.youtube.com/watch?v=IolxqkL7cD8
        username = os.environ.get('my_facebook_username')
        password = os.environ.get('my_facebook_password')

        driver.find_element_by_name("email").send_keys(username)
        driver.find_element_by_name("pass").send_keys(password)
        driver.find_element_by_name("login").click()
        print(input("Press any Key: "))
        print("Login work Successfully ")

    except:
        pass


def navigatePagePostAria():
    sleepTime = 4
    implicitlyWaitTime = 20
    for i in range(2):
        driver.implicitly_wait(implicitlyWaitTime)
        actions.send_keys(Keys.BACK_SPACE)
        actions.send_keys(Keys.TAB * 4)
        time.sleep(sleepTime)
        actions.perform()
        print("Firast 10 tabs Working")

    actions.send_keys(Keys.TAB * 3)
    actions.send_keys(Keys.ENTER)
    actions.perform()
    print("Navigate Post area Successfully ")


chrome_options()
driver()
login()
driver.get("https://www.facebook.com/groups/402353916617590/permalink/1630582000461436/")
navigatePagePostAria()

Error Come from last function navigatePagePostAria() The Line will be actions.send_keys(Keys.BACK_SPACE)

error :

Traceback (most recent call last):
  File "K:\Project\Python\Miracle\groupPost.py", line 154, in <module>
    navigatePagePostAria()
  File "K:\Project\Python\Miracle\groupPost.py", line 63, in navigatePagePostAria
    actions.send_keys(Keys.BACK_SPACE)
AttributeError: 'function' object has no attribute 'send_keys'

This two line is the most important like for navigation

1.actions.send_keys(Keys.BACK_SPACE)

2.actions.send_keys(Keys.TAB * 4)

I use that as a alternative of x path bechouse facebook is very dynamic its change its x path every minits.

Video Description of my Problem: https://youtu.be/BzSBLAaYS-s

1 Answers

The issue here is that you are using misleading names for your methods.
You defined the following method

def actions():
    global actions
    actions = ActionChains(driver)

so that now when you calling actions.send_keys(Keys.BACK_SPACE) the interpreter thinks you are calling that actions method, not the actions object initiated inside it.
I would recommend you define this method as following

def set_actions():
    global actions
    actions = ActionChains(driver)

or even better not defining this method at all since this is not really method you are going to call several times.
The same about chrome_options and driver methods.
There is no need to define them as methods since these are not reusable methods, it could better be a flat code, so that the entire your code will look as following:

import os
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

#Setting the chrome_options
global chrome_options
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--profile-directory=Default')
# chrome_options.add_argument("--user-data-dir=chrome-data")
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('disable-infobars')
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

#Setting the Chrome Driver
global driver
driver = webdriver.Chrome("chromedriver.exe", chrome_options=chrome_options)

#Setting the Actions
global actions
actions = ActionChains(driver)


def login():
  driver.get("https://facebook.com")
    try:
        # I use environment variable  base on this tutorials https://www.youtube.com/watch?v=IolxqkL7cD8
        username = os.environ.get('my_facebook_username')
        password = os.environ.get('my_facebook_password')

        driver.find_element_by_name("email").send_keys(username)
        driver.find_element_by_name("pass").send_keys(password)
        driver.find_element_by_name("login").click()
        print(input("Press any Key: "))
        print("Login work Successfully ")

    except:
        pass


def navigatePagePostAria():
    sleepTime = 4
    implicitlyWaitTime = 20
    for i in range(2):
        driver.implicitly_wait(implicitlyWaitTime)
        actions.send_keys(Keys.BACK_SPACE)
        actions.send_keys(Keys.TAB * 4)
        time.sleep(sleepTime)
        actions.perform()
        print("Firast 10 tabs Working")

    actions.send_keys(Keys.TAB * 3)
    actions.send_keys(Keys.ENTER)
    actions.perform()
    print("Navigate Post area Successfully ")


login()
driver.get("https://www.facebook.com/groups/402353916617590/permalink/1630582000461436/")
navigatePagePostAria()

I also migrated the driver.get("https://facebook.com") into the login method since this belongs there more that to the basic initialization of the selenium webdriver itself

Related