0

I am trying to use this python code from here. I am using firefox geckodriver instead. I get an index error from line 43 which is log_in[0].click(). Here is the code for convenience:

# importing necessary classes
# from different modules
from lib2to3.pgen2 import driver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox()
prefs = {"profile.default_content_setting_values.notifications": 2}


# open facebook.com using get() method
browser.get('https://www.facebook.com/')

# user_name or e-mail id
username = "argleblargle@gmail.com"

# getting password from text file
with open('test.txt', 'r') as myfile:
    password = myfile.read().replace('\n', '')

print("Let's Begin")

element = browser.find_elements_by_xpath('//*[@id ="email"]')
element[0].send_keys(username)

print("Username Entered")

element = browser.find_element_by_xpath('//*[@id ="pass"]')
element.send_keys(password)

print("Password Entered")

# logging in
log_in = browser.find_elements_by_id('loginbutton')
log_in[0].click()

print("Login Successful")

browser.get('https://www.facebook.com/events/birthdays/')

feed = 'Hap Borth! Hope you have an amazing day!'

element = browser.find_elements_by_xpath("//*[@class ='enter_submit\
    uiTextareaNoResize uiTextareaAutogrow uiStreamInlineTextarea\
                inlineReplyTextArea mentionsTextarea textInput']")

cnt = 0

for el in element:
    cnt += 1
    element_id = str(el.get_attribute('id'))
    XPATH = '//*[@id ="' + element_id + '"]'
    post_field = browser.find_element_by_xpath(XPATH)
    post_field.send_keys(feed)
    post_field.send_keys(Keys.RETURN)
    print("Birthday Wish posted for friend" + str(cnt))

# Close the browser
browser.close()

As you can see from the code, it prints out when a step is completed. It passed username entered, passed password entered, but did not pass login successful. I get an IndexError: line 43, in <module> log_in[0].click()

Is that because the login button is somewhere different from when the code was first written? Is it 2FA shenanigans? I am doing this for fun, thanks for reading.

EDIT: the original error was because of the s in ind_elements_by_id. There is one element. Oops.

The error is now selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="loginbutton"]

1 Answers1

0

log_in variable is empty, because many websites including facebook first load the website as such, and only than load the layout and all the elements with javascript.

Your code tries to interact with facebook before it is fully loaded and therefor cannot find login button.

You can do something like this to resolve your problem or just write a while loop that checks if the button is found.

wikwoj
  • 203
  • 2
  • 6