3

I have been trying to write a selenium script to login to my Quora account.

This is the script I have written.

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

email=raw_input("email: ")
password=getpass.getpass("Password: ")

driver = webdriver.Firefox()
driver.get("http://www.quora.com")

#time.sleep(5)

Form=driver.find_element_by_xpath("//div[@class='form_inputs']")

Form.find_element_by_name("email").send_keys(email)

#time.sleep(4)

Form.find_element_by_name("password").send_keys(password)

#time.sleep(4)

Form.find_element_by_xpath("//input[@value='Login']").click()

The statement Form=driver.find_element_by_xpath("//div[@class='form_inputs']") takes very long to find the element. In fact, all the find_element statements take very long to do their job.(This could be because of some Javascript snippet to increase the load on selenium, but I could not understand much from the page source)

Is there any way I could do it faster? Similar scripts have worked well for me in Facebook and Google.

EDIT: Removed the time.sleep() calls. It still takes around 6-8 minutes to find the element.

rmunn
  • 34,942
  • 10
  • 74
  • 105
viv1
  • 99
  • 2
  • 10

2 Answers2

1

The reason why it is taking a while is because you are preforming time.sleep()

You should not do this, it's bad practice. You should be using WebDriver waits. I would personally go with Implicit waits for your scenario. Please see the documentation

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • I have tried without the waits and it still takes a long time(like 6-8 minutes) even without the time.sleep call. – viv1 Jan 26 '15 at 14:04
  • I am going to suggest there is something wrong with the actual driver implementation then since the code you have posted is not designed to last that long. You would be experiencing NotSuchElementException. Have you tried using the latest drivers? – Jamie Rees Jan 26 '15 at 14:09
  • I checked , and it turns out I am using the latest driver. Similar scripts work for me in other sites (like facebook, etc). Also, interestingly, there is no error, just delay. The element is found by the script, only after a long time. – viv1 Jan 26 '15 at 14:24
1

This is something I've seen here asked on SO multiple times, see:

I've been able to reproduce the slow code execution using Firefox, but the following code works without any delays using Chrome or PhantomJS driver:

import getpass

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

email = raw_input("email: ")
password = getpass.getpass("Password: ")

driver = webdriver.Chrome()
driver.get("http://www.quora.com")

form = driver.find_element_by_class_name('regular_login')
form.find_element_by_name('email').send_keys(email)
form.find_element_by_name('password').send_keys(password + Keys.RETURN)

FYI, for Firefox, it helps to overcome the issue if you fire up Firefox with disabled javascript:

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("browser.download.folderList",2)
firefox_profile.set_preference("javascript.enabled", False)

driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get('http://www.quora.com/')

But, as you would see - you'll quickly get a different set of problems.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I went through the links and unfortunately( as you would have seen) no one has been able to come up with a working solution on Firefox driver. The best alternative currently, I guess, is to switch to headless browsers. I haven't tried with Chrome drivers yet, but the find_element was quite fast with PhantomJS. – viv1 Jan 27 '15 at 08:46
  • @spiderbat yup, for some reason Firefox is behaving slow (even the latest). I think it is pretty much a good workaround to use `Chrome` or `PhantomJS`. – alecxe Jan 27 '15 at 08:53