Skip to content

Instantly share code, notes, and snippets.

@hugs
Created February 16, 2011 19:40
Show Gist options
  • Save hugs/830011 to your computer and use it in GitHub Desktop.
Save hugs/830011 to your computer and use it in GitHub Desktop.
Example code for using the Selenium 2 Python bindings.
# To install the Python client library:
# pip install -U selenium
# Import the Selenium 2 namespace (aka "webdriver")
from selenium import webdriver
# iPhone
driver = webdriver.Remote(browser_name="iphone", command_executor='http://172.24.101.36:3001/hub')
# Android
driver = webdriver.Remote(browser_name="android", command_executor='http://127.0.0.1:8080/hub')
# Google Chrome
driver = webdriver.Chrome()
# Firefox
driver = webdriver.Firefox()
# ------------------------------
# The actual test scenario: Test the codepad.org code execution service.
# Go to codepad.org
driver.get('http://codepad.org')
# Select the Python language option
python_link = driver.find_elements_by_xpath("//input[@name='lang' and @value='Python']")[0]
python_link.click()
# Enter some text!
text_area = driver.find_element_by_id('textarea')
text_area.send_keys("print 'Hello,' + ' World!'")
# Submit the form!
submit_button = driver.find_element_by_name('submit')
submit_button.click()
# Make this an actual test. Isn't Python beautiful?
assert "Hello, World!" in driver.get_page_source()
# Close the browser!
driver.quit()
@santoshssit
Copy link

hi i am running on Windows can u please explain me how can i install python client for selenium 2.0 and where i get that

@santoshssit
Copy link

Can you please give me the steps to use this ITS very urgent

@hugs
Copy link
Author

hugs commented May 24, 2011

You can download the latest Python client from http://pypi.python.org/pypi/selenium. In Windows, at a command prompt in a terminal window try: "pip.exe install -U selenium" or "easy_install.exe install -U selenium". If you have trouble, google for how to install Pip or Easy Install.

@troy999
Copy link

troy999 commented Jul 3, 2011

Running Python 2.6.1 I am receiving the following error:

driver = webdriver.Remote(browser_name="iphone", command_executor='http://:3001/hub')
Traceback (most recent call last):
File "", line 1, in
TypeError: init() got an unexpected keyword argument 'browser_name'


Any help would be greatly appreciated...

@ameyspatil
Copy link

self.driver = webdriver.Remote(command_executor='http://192.168.1.109:3001/hub', desired_capabilities={'browserName': 'iphone'})
this should make it work...

@gingertea
Copy link

I am on a network behind a proxy. When i run this script, i get a new instance of Firefox with no proxy configured, that obviously fails to connect to any external site. I tried setting http_proxy env variable from shell but that didn't work. On the WebDriver FAQ on code.google.com, i found this Java code:

Proxy proxy = new Proxy();
proxy.setProxyAutoconfigUrl("http://youdomain/config");

// We use firefox as an example here.
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);

// You could use any webdriver implementation here
WebDriver driver = new FirefoxDriver(capabilities);

How do i translate this to Python?

@ostapel
Copy link

ostapel commented Feb 27, 2012

I have installed all the staff, but after opening FF nothing happens, proxy is OK, but driver.get("http://google.com") does not open page - no actions are performed, just starting page....anybody knows why ?

@martinhbramwell
Copy link

I gotta say ... I REALLY appreciated stumbling onto this! Thank you!!

I was facing a need to use Selenium on one project and a need to start learning Python for another. Doing both together seemed like a good idea, but I figured I'd need several days to figure out how to do it. Bingo! You got me up and running in 10 minutes! Great!

one small thing though -- "AttributeError: 'WebDriver' object has no attribute 'get_page_source'"

After a quick Google around, I found that this works, instead -- assert "Hello, World!" in driver.page_source

@jollychang
Copy link

there is some update maybe

from selenium import webdriver
capabilities = webdriver.DesiredCapabilities()
driver = webdriver.Remote(command_executor='http://127.0.0.1:8080/wd/hub', desired_capabilities=capabilities.ANDROID)
driver.get('http://www.douban.com')

@jakeyeung
Copy link

This is indeed very beautiful. Thank you!

@SureshKL
Copy link

SureshKL commented Jul 3, 2014

Worked fine for Chrome and Firefox,
But getting the below ERROR when I run the script for Android on Android 4.1.2 rooted phone.
Traceback (most recent call last):
File "C:/Users/SURESH/Downloads/gist830011-f40c4473d6e73fbc22d0c9ad3ab2d3f6e9f1be70/selenium-examples.py", line 11, in
driver = webdriver.Remote(browser_name="android", command_executor='http://127.0.0.1:8080/hub')
TypeError: init() got an unexpected keyword argument 'browser_name'

@asmuth444
Copy link

hi i was working on a python script once we use the click function and reach an other page then how can i continue the same process of searching and clicking on the next page?

@sudhir0015
Copy link

Hi.. When i am trying to execute this script, i am getting error :
Traceback (most recent call last):
File "C:\Python27\selenium\selenium-Example.py", line 42, in
assert "Hello, World!" in driver.get_page_source()
AttributeError: 'WebDriver' object has no attribute 'get_page_source'

How to get over from this error???

@sonampriya404
Copy link

if after clicking submit ,i am going to a link,so how do i write ?

@ronbarak
Copy link

ronbarak commented Nov 4, 2016

After running:

$ Xvfb :99 &
$ export DISPLAY=:99

I made several changes to make the code from GitHub run (on Ubuntu/FireFox).
Following is my changed code:

# To install the Python client library:
# pip install -U selenium

from __future__ import print_function

# Import the Selenium 2 namespace (aka "webdriver")
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# iPhone
# driver = webdriver.Remote(browser_name="iphone", command_executor='http://172.24.101.36:3001/hub')
# Android
# driver = webdriver.Remote(browser_name="android", command_executor='http://127.0.0.1:8080/hub')
# Google Chrome 
# driver = webdriver.Chrome()

# Firefox 
# driver = webdriver.Firefox()

# per http://stackoverflow.com/questions/37761668/cant-open-browser-with-selenium-after-firefox-update/37765661
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities)

# ------------------------------
# The actual test scenario: Test the codepad.org code execution service.

# Go to codepad.org
driver.get('http://codepad.org')

# Select the Python language option
python_link = driver.find_elements_by_xpath("//input[@name='lang' and @value='Python']")[0]
python_link.click()

# Enter some text!
text_area = driver.find_element_by_id('textarea')
text_area.send_keys("print 'Hello,' + ' World!'")

# Submit the form!
submit_button = driver.find_element_by_name('submit')
submit_button.click()

# Make this an actual test. Isn't Python beautiful?
#assert "Hello, World!" in driver.get_page_source()
html_source = driver.page_source
print ("html_source:",html_source)
hello_in_html = "Hello, World!" in html_source

# Close the browser!
driver.quit()

assert hello_in_html

@subasss
Copy link

subasss commented Jan 11, 2018

How to find drop down list in python selenium

@skpatro
Copy link

skpatro commented Feb 22, 2018

driver.get("https://skpatro.github.io/demo")
driver.find_element_by_link_text("SignUp Form").click()

select = Select(driver.find_element_by_name("sgender"))
select.select_by_value("male")
print("Selected option " + select.first_selected_option.text)
opts = select.options
print("Available options are")
for opt in opts:
    print(opt.text)
time.sleep(3)
driver.quit()

@aniketmule
Copy link

Updated the code as I got error with the above example due to change in element name.


# To install the Python client library:
# pip install -U selenium

# Import the Selenium 2 namespace (aka "webdriver")
from selenium import webdriver

# iPhone
driver = webdriver.Remote(browser_name="iphone", command_executor='http://172.24.101.36:3001/hub')

# Android
driver = webdriver.Remote(browser_name="android", command_executor='http://127.0.0.1:8080/hub')

# Google Chrome 
driver = webdriver.Chrome()

# Firefox 
driver = webdriver.Firefox()

# ------------------------------
# The actual test scenario: Test the codepad.org code execution service.

# Go to codepad.org
driver.get('http://codepad.org')

# Select the Python language option
python_link = driver.find_elements_by_xpath("//input[@name='lang' and @value='Python']")[0]
python_link.click()

# Enter some text!
text_area = driver.find_element_by_id('textarea')
text_area.send_keys("print 'Hello,' + ' World!'")

# Submit the form!
submit_button = driver.find_element_by_class_name('g-recaptcha')
submit_button.click()

# Make this an actual test. Isn't Python beautiful?
assert "Hello, World!" in driver.get_page_source()

# Close the browser!
driver.quit()

@ernstki
Copy link

ernstki commented Oct 17, 2018

With the PhantomJS WebDriver being deprecated nowadays, it's handy to know how to use the Chrome WebDriver headless.

Here's how to do that:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('headless')

driver = webdriver.Chrome(chrome_options=options)

Source: https://intoli.com/blog/running-selenium-with-headless-chrome/

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