0

Program: Python + Selenium

Situation: I am trying to run a script to automate Facebook login and open my Facebook home page

Problem: I am a beginner and i am not sure if there is a problem in my python configuration or i am just doing a code mistake. the code is stopping at the last line. How did I retrieve the element class? i went to Facebook page and i inspected the profile button element and i copied it but it is not working (_1vp5 f_click)

Resolution: could you please direct me on the best way to find the home page element

Below is my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.get("http://www.facebook.com")

usr = "xxxxx"
pwd = "yyyy"

assert "Facebook" in driver.title
elem = driver.find_element_by_id("email")

elem.send_keys(usr)
elem = driver.find_element_by_id("pass")

elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)

driver.find_element_by_class("_1vp5 f_click").click()

Code output:

Traceback (most recent call last):
File "C:/Users/shouks/MyPythonScripts/test1.py", line 23, in <module>
driver.find_element_by_class("_1vp5 f_click").click()
AttributeError: 'WebDriver' object has no attribute 'find_element_by_class'
6
  • 1
    Try driver.find_element_by_class_name("_1vp5 f_click").click() Commented Mar 16, 2018 at 13:24
  • Possible duplicate of Logging Facebook using selenium Commented Mar 16, 2018 at 13:25
  • 1
    Using any such automation on their site is against Facebook’s ToS. “I am trying to run a script to automate Facebook login and open my Facebook home page” - to what end, what’s the purpose of that? Commented Mar 16, 2018 at 13:35
  • @CBroe Just out of curiosity, I checked Facebook's ToS here: facebook.com/terms.php Are you sure this type of automation is actually violating their ToS? Maybe I missed it, but I haven't found anything that would explicitly forbid this. Commented Mar 16, 2018 at 13:56
  • @JoeT.Boka 3.2, “You will not collect users' content or information, or otherwise access Facebook, using automated means (such as harvesting bots, robots, spiders, or scrapers) without our prior permission.” I think it would be fair to say that “or otherwise access Facebook, using automated means” covers what’s attempted here. Plus, in all my time here I have hardly ever seen a use-selenium-on-facebook question that I would not consider “shady at best” to begin with. Usually its something idi*tic like “I want to like all posts on my timeline automatically.” Commented Mar 16, 2018 at 14:04

1 Answer 1

2

There is no such method find_element_by_class.

Use this method instead: find_element_by_class_name

http://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.