0

This is my code:

from selenium import webdriver

import time

driver = webdriver.Chrome()
driver.get("https://www.fb.com")
driver.find_elements_by_name("email").send_keys("******************")
driver.close()

Observation: After execute the program I get error. send_keys() and click() methods are not working.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
vivek
  • 11
  • 2
  • Does this answer your question? [sendkeys are not working in Selenium Webdriver](https://stackoverflow.com/questions/20936403/sendkeys-are-not-working-in-selenium-webdriver) – Banana Sep 18 '20 at 06:00
  • What kind of errors show the error message. – Arundeep Chohan Sep 18 '20 at 06:07
  • Traceback (most recent call last): File "/home/dhanvika/PycharmProjects/Italy/sss.py", line 9, in driver.find_elements_by_name("email").send_keys("******************") AttributeError: 'list' object has no attribute 'send_keys' – vivek Sep 18 '20 at 06:28
  • That was due to find_elements use find_element. – Arundeep Chohan Sep 18 '20 at 06:37

4 Answers4

0

The error is coming because of this line driver.find_elements_by_name("email"). The find_elements command in selenium is used to extract multiple elements with same kind of locators which return a list of objects.

Over here in you case, you are accessing a single element and for the extraction you need to use driver.find_element_by_name("email").

A sample script is given below:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.fb.com")
driver.find_element_by_name("email").send_keys("******************")
driver.close()

Just a additional information, close() command closes the window under focus and quit() command closes window active in session. So you can use driver.quit() command either.

0

To get an element after travelling to page we tend to wait for an element to be interactable due to page loading wait time. You also use find_elements and not find_element.

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 import webdriver

driver = webdriver.Chrome()
driver.get("https://www.fb.com")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "email"))).send_keys("*****")
driver.quit()
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

Three issues:

  • you need to find one element not many elements
  • you need to give time for things to load
  • don't need to close the driver straight away

try:

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

driver = webdriver.Chrome()

driver.get("https://www.fb.com")
time.sleep(10)
driver.find_element_by_name("email").send_keys("******************")
time.sleep(1)
driver.find_element_by_name("password").send_keys("******************")
time.sleep(1)
driver.find_element_by_name("password").(keys.Keys.RETURN)
John Moody
  • 81
  • 10
0

You need to take care of a lot of things here as follows:

  • find_elements_by_name() returns a list with elements if any was found or an empty list if not. But moving further as you are trying to invoke send_keys(*value) so you must be looking for an WebElement and use find_element_by_name(name) instead which Finds an element by name.

  • You haven't used any method from the time module so ideally you need to remove the unused import:

    import time
    
  • Invoking https://www.fb.com through get() inturn invokes https://www.facebook.com/. But scraping Facebook is against the TOS 3.2 and you are liable to be questioned and may even land up in Facebook Jail. Use Facebook Graph API instead.

  • Facebook is built through ReactJS which is pretty much evident from the presence of the following keywords and tags within the HTML DOM:

    • {"react_render":true,"reflow":true}
    • <!-- react-mount-point-unstable -->
    • ["React-prod"]
    • ["ReactDOM-prod"]
    • ReactComposerTaggerType:{r:["t5r69"],be:1}

    So, the dynamically generated class names are bound to change after certain timegaps and you should avoid automating Facebook.

  • Still if you intend to automate logging into Facebook you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("vivek")
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='email']"))).send_keys("vivek")
      
    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352