How To Use Selenium With Python

How To Use Selenium With Python

One of the real advantages of Selenium is that there are binding for all of the most popular languages, Python being one of them! The Python bindings are among the most intuitively written and the documentation surrounding the binding is among the most complete. While all of the Selenium bindings are similar, there are small differences that can make getting your first test running in a new language frustrating. To alleviate these issues I have provided a few of the most commonly used commands using the Python binding.

Most Commonly Used Commands :

Instantiating the Remote WebDriver

  • driver = webdriver.Remote( command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities)

Finding an Element:

  • driver.find_element_by_name(“name”)
  • driver.find_element_by_id(“id”)
  • driver.find_element_by_xpath(“xpath”)
  • driver.find_element_by_link_text(“link text”)
  • driver.find_element_by_partial_link_text(“partial link text”)
  • driver.find_element_by_tag_name(“tagname”)
  • driver.find_element_by_class_name(“className”)
  • driver.find_element_by_css_selector(“selector”)

Interacting with Elements

  • element.send_keys("Typing")
  • element.clear()
  • element.click()
  • element.submit()
  • element.is_displayed()
  • element.is_enabled()

Ending the WebDriver Session:

  • driver.close()
  • driver.quit()

To run a Python Selenium test you can simply use the command "python my_file.py" in the terminal, provided the libraries are imported correctly. To run tests without the trouble of an in-house grid Sauce Labs is an excellent alternative. 

Why Sauce Labs?

  • Cloud based Selenium grid with over 800+ browser and OS combinations
  • Test behind your firewall with Sauce Connect, a secure tunneling tool
  • Eliminate infrastructure maintenance

Get our Selenium Bootcamp guide and get started with testing today!