Skip to content

Instantly share code, notes, and snippets.

@leostera
Created August 30, 2012 17:58
Show Gist options
  • Save leostera/3535568 to your computer and use it in GitHub Desktop.
Save leostera/3535568 to your computer and use it in GitHub Desktop.
Selenium WebDriver Python Bindings and Facebook Login
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
usr = ""
pwd = ""
driver = webdriver.Firefox()
# or you can use Chrome(executable_path="/usr/bin/chromedriver")
driver.get("http://www.facebook.org")
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)
elem = driver.find_element_by_css_selector(".input.textInput")
elem.send_keys("Posted using Python's Selenium WebDriver bindings!")
elem = driver.find_element_by_css_selector("input[value=\"Publicar\"]")
elem.click()
driver.close()
@sguha
Copy link

sguha commented Jan 24, 2016

@suprith4989 I would do this

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

import unittest

class LoginTest(unittest.TestCase):

def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.get("https://www.facebook.com/")

def test_Login(self):
    driver = self.driver
    facebookUsername = "[email protected]"
    facebookPassword = "Passw0rd123"
    emailFieldID = "email"
    passFieldID = "pass"
    loginButtonXpath = "//input[@value='Log In']"
    facebookLogo = "/html/body/div/div[1]/div/div/div/div[1]/div/h1/a"

    emailFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id(emailFieldID))
    passFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id(passFieldID))
    loginButtonElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))

    emailFieldElement.clear()
    emailFieldElement.send_keys(facebookUsername)
    passFieldElement.clear()
    passFieldElement.send_keys(facebookPassword)
    loginButtonElement.click()
    WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(facebookLogo))

def tearDown(self):
    self.driver.quit()

if name == 'main':
unittest.main()

@ravilasadov
Copy link

How can I use assert/verify for this same test to check the name of the user (assert that the username is visible)?

@vivekiyer114
Copy link

Please someone tell me what is the use of this line ?

elem = driver.find_element_by_css_selector(".input.textInput")

And....this ...
elem = driver.find_element_by_css_selector("input[value="Publicar"]")

@saidihasan
Copy link

@vivekiyer114 : according to this http://selenium-python.readthedocs.io/api.html driver.find_element_by_css_selector for search and match from css website.

@jebastin4u
Copy link

jebastin4u commented Feb 19, 2017

I got this ERROR

File "new_login.py", line 17, in
elem = driver.find_element_by_css_selector(".input.textInput")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 437, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .input.textInput

@srcecde
Copy link

srcecde commented Feb 27, 2017

Hi,

I have wrote the script that automates the Facebook login, status updates and likes (can select the user). Have a look at GitHub

Thanks

@HarishGargIndia
Copy link

HarishGargIndia commented Sep 30, 2017

Thank you so much i am able to login my facebook account by python. but can you tell me what does "assert "Facebook" in driver title" mean.
i am very new to python.
i am running below code to login one site named - Trivago could anyone help me.
code..

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

usr = "Trivago"
pwd = "123"

driver = webdriver.Chrome("F:\Training\Python\chromedriver.exe")
driver.get("https://intelligence.trivago.com")
#assert "Trivago" 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)

error -
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"email"}
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7600 x86_64)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment