Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriverWait returns an error on Python while web scraping

I am creating a script for Facebook scraping. I am using Selenium 2.53.6, Gecko driver 0.11.1 with Firefox 50 under Ubuntu 14.04.5 LTS. I have also tried it under Windows 10 using Gecko as well as Chromedriver too, with the same results (that I will describe below) :(

The following code fragment I use that I copied from the original documentation in section Explicitly Waits is:

import datetime, time, sys, argparse
from time import strftime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
...
...
def main(usrEmail, pwd, numberOfScrolls, secondsWait, searchItem, outFilename):
    # Open Firefox Browser
    binary = FirefoxBinary('/usr/bin/firefox')
    browser = webdriver.Firefox(firefox_binary=binary)
    
    #put browser in specific position
    browser.set_window_position(400, 0)

    # goto facebook
    browser.get("http://www.facebook.com")
    
    #waiting( 5 )
    try:
        element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "u_0_p")))
    finally:
        logging("logging in...")

The problem is that I get this error:

  File "fb.py", line 86, in main
    element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "u_0_p")))
NameError: name 'By' is not defined

As a workaround I am just using delays in a form time.sleep( delay ) and the program works pretty nice, but it would be better if I had the WebDriverWait feature.

Do I miss something obvious or it is a kind of selenium bug or a web browser feature? Any help / clarification will be greatly appreciated!

Thanks for your time!!

like image 762
Andreas Venieris Avatar asked Mar 22 '26 23:03

Andreas Venieris


1 Answers

Could be your missing the definition of By:

from selenium.webdriver.common.by import By
like image 84
Moshisho Avatar answered Mar 24 '26 17:03

Moshisho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!