8

I'm using Selenium and chrome webdriver but when I run scripts it opens a window. Is there any way that it can access the internet without the window popping up?

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

driver = webdriver.Chrome()

driver.get("https://ps.rsd.edu/public/")
elem = driver.find_element_by_name("account")
elem.send_keys("Username")
elem2 = driver.find_element_by_name("pw")
elem2.send_keys("Password")
elem.send_keys(Keys.RETURN)

driver.quit()

For example, this goes to my school's grade site and puts in a username and password but I want to do this without the browser popping up if that's possible.

Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
Serial
  • 7,925
  • 13
  • 52
  • 71
  • 2
    If you install pyvirtualdisplay (a wrapper for Xvfb), then you can run Selenium in a headless (virtual) display. [Corey Goldberg shows a nice example](http://coreygoldberg.blogspot.com/2011/06/python-headless-selenium-webdriver.html) of how to do this. – unutbu May 05 '13 at 22:59
  • That will work perfectly thank you!!!! – Serial May 05 '13 at 23:05
  • i cant get it to work :( – Serial May 05 '13 at 23:20

1 Answers1

16

I would suggest try using headless PhantomJs GhostDriver (which is a relatively new thing). As this is the native Selenium Webdriver way of doing it.

Download PhantomJs executables from http://phantomjs.org/download.html.

driver = webdriver.PhantomJS("./phantomjs") # path to phantomjs binary
driver.get("https://ps.rsd.edu/public/")

elem = driver.find_element_by_name("account")
elem.send_keys("Username")
elem2 = driver.find_element_by_name("pw")
elem2.send_keys("Password")
elem.send_keys(Keys.RETURN)

driver.quit()
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • 1
    Im getting a long error message here is the end of it: `raise WebDriverException("Unable to start phantomjs with ghostdriver.", e) WebDriverException: Message: 'Unable to start phantomjs with ghostdriver.' ; Screenshot: available via screen ` – Serial May 05 '13 at 23:25
  • 3
    @ChristianCareaga Can you please say how you figured out how to deal with that error? That's what's coming up for me now. Thanks. – DJG Jun 24 '13 at 10:49
  • i had to write the full directory to phantomjs i think , but im not sure i wrote this awhile back and ended up doing something else – Serial Jun 24 '13 at 19:12
  • I'm also getting "Unable to start phantomjs with ghostdriver." The dev mentioned in a comment hasn't updated the python 2.7 selenium bindings. They're broken or something. – Alkanshel Nov 16 '13 at 05:09
  • I was getting the same error, but manually implementing the [fix found here](https://code.google.com/p/selenium/source/detail?r=977c310569539f00c4090db244276e224d9884b3) did the trick. It will be included in 2.40 when that comes out. – Tetrinity Feb 21 '14 at 10:24
  • I was having some issues, even with selenium 2.40. Once I passed in the full path to the `phantomjs.exe`, everything worked fine. – blachniet Mar 03 '14 at 14:40