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'
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
shouks
  • 13
  • 3
  • 1
    Try `driver.find_element_by_class_name("_1vp5 f_click").click()` – Joe T. Boka Mar 16 '18 at 13:24
  • Possible duplicate of [Logging Facebook using selenium](https://stackoverflow.com/questions/45635190/logging-facebook-using-selenium) – undetected Selenium Mar 16 '18 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? – CBroe Mar 16 '18 at 13:35
  • @CBroe Just out of curiosity, I checked Facebook's ToS here: https://www.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. – Joe T. Boka Mar 16 '18 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.” – CBroe Mar 16 '18 at 14:04
  • @CBroe Got it! I completely missed this part. I was just reading through it fast. In any case, most marketers use Facebook to try to sell something, so most interactions are fake anyway. – Joe T. Boka Mar 16 '18 at 14:34

1 Answers1

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

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65