Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
@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

aniketmule commented Apr 25, 2018

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