Selenium Python Cheat Sheet

Posted on  by 



Frequently used java selenium commands – Cheat Sheet. Visit python selenium commands cheat sheet here. Driver setup: Firefox: System.se­tPr­ope­rty­(“we­bdr­ive­r.g­eck­o.d­riv­er”, “­Pat­h To­ g­eck­odr­ive­r”). Selenium Q&A https: //w ww.u de my.c om /co urs e/c ert ifi ed- sel eni um- tes ter -fo und ation/ ISTQB Advanced Test Automation Engine ering.

  • Selenium Webdriver CheatSheet. GitHub Gist: instantly share code, notes, and snippets.
  • Here you will learn how to control any browser application with a little Python and selenium. You can use this for creating your own Instagram Bot for example. We start with a few basics and end with a cheat sheet summarizing the most important code snippets.
  • This cheat sheet has examples on common methods used in Selenium Automation. Suggestion is to learn Core Java before moving to Selenium Automation. If you already know Java then Selenium is peanut for you. Although Selenium supports various programming languages (C#, Java, Perl, PHP, Python & Ruby etc.) but this cheat sheet has exmples using only Java.

Here you will learn how to control any browser application with a little Python and selenium. You can use this for creating your own Instagram Bot for example. We start with a few basics and end with a cheat sheet summarizing the most important code snippets. Let’s go!

One way to automate the tasks that are performed in the browser is to use a program that “records” your activities on the website and can play them back as often as you like. No coding is needed for this. Correspondingly stupidly the program executes its steps.

You can find a Chrome extension here, that allows you to record and play your tasks. Now this works for logging into Instagram well, but we need some kind of programming to make our automation a bit smarter.

Below I have added an example of how such a login via Selenium might look like. He notes all necessary HTML IDs to be able to access them directly later. If Instagram should change the IDs at a later time, the user will surely not notice it. The bot will still not find the individual parts again and will fail.

First of all. We will not directly control the browser. We will write a script that uses a web driver to control a browser. For example, our Python script will use a ChromeDriver to control the web browser Google Chrome.

So we need Selenium and a Webdriver.

  • Selenium: You can install selenium via “pip3 install selenium” inside for example Powershell.
  • Webdriver: You can download it from the SeleniumHQ Website for your preferred Browser.

When using the program, you can set the path to the driver like this (Old way):

There is a better way to start. Create a new python Script and let it download and set chromedriver by itself. For that, we need “webdriver manager”:

Is this installed, you can now create a little Python file and run it to open a specific Website. In my case Instagram:

Horray we now can open a Website, let’s see how we can log in.

You can see the whole HTML Code right inside the Browser. Just right click and click “inspect”. You can also press “Ctrl + Shift + I”. You can use this to see all the IDs and points we will use to automate.

In Python, you now can use the “driver.find_element” to address exactly one spot. By their specific name or class or ID. Whatever suits you the most:

In my case, the Button doesn’t have any ID. So I have to target it via the “XPath” and search for the text inside the button. Each element has its own path. We can either search using the absolute path, which would result in errors if we changed the website structure. Or we search within our specified path for a word. Here the structure of the website can change as much as it wants, our word is always found.

In our case, it’s the word “next”. This way we can make sure that we notice when Instagram changes something as soon as possible because you have to change it so that it is changed for the user.

How to find your XPath and use it inside your Script

You now can find and click the button via the text the user can see, or via the XPath, you have copied above. The following Script will open Instagram, and waits 5 seconds till it clicks the Next Button. Of course, it won’t log in, because there is no data typed in.

xxx will be your variable which contains the XPath or name. In our Example above it would be “python_button”.

Radio Buttonxxx.click()
Click Checkbox
Click Button
Textfieldxxx.send_keys(“Hello World!”)
Getting Textxxx.get_attribute(“text”)
Dropdown by visible textxxx.select_by_visible_text(‘Germany’)
Dropdown by valuexxx.select_by_value(‘1’)


For Python version the link is here


Install Java

To install Java go to this link


IDE

You can use any text editor. I recommend Eclipse as it is free and have extensive support. For list of popular editors , this are the links


Download Selenium

Python

Download selenium webdriver in this link


Dirty our hands !


Import Selenium



Browsers support (Firefox , Chrome , Internet Explorer, Edge , Opera)

Driver setup:

Chrome:

System.se­tPr­ope­rty­('we­bdr­ive­r.chrome.d­riv­er', “'Pat­h To­ chromedr­ive­r');

To download: Visit Here


Firefox:

System.se­tPr­ope­rty­('we­bdr­ive­r.g­eck­o.d­riv­er', '­Pat­h To­ g­eck­odr­ive­r');

To download: Visit GitHub


Internet Explorer:

System.se­tPr­ope­rty­('we­bdr­ive­r.ie.d­riv­er', 'Pat­h To­ IEDriverServer.exe');

To download: Visit Here

Edge:

System.se­tPr­ope­rty­('we­bdr­ive­r.edge.d­riv­er', 'Pat­h To­ MicrosoftWebDriver.exe');

To download: Visit Here


Opera:

System.se­tPr­ope­rty­('we­bdr­ive­r.opera.d­riv­er', 'Pat­h To­ operadriver');

To download: visit GitHub

Dark knight sub. Browser Arguments:

–headless

To open browser in headless mode. Works in both Chrome and Firefox browser

–start-maximized

To start browser maximized to screen. Requires only for Chrome browser. Firefox by default starts maximized

–incognito

To open private chrome browser

–disable-notifications

To disable notifications, works Only in Chrome browser

Example:


Alternative


Launch URL


Retrieve Browser Details:


Navigation


Locating Elements

By id

<input id=”login” type=”text” />


By Class Name

<input class=”gLFyf” type=”text” />


By Name

<input name=”z” type=”text” />


By Tag Name

<div id=”login” >…</div>


By Link Text

<a href=”#”>News</a>


By XPath

<form id=”login” action=”submit” method=”get”>

Username: <input type=”text” />

Password: <input type=”password” />

</form>


By CSS Selector

<form id=”login” action=”submit” method=”get”>

Username: <input type=”text” />

Password: <input type=”password” />

</form>


Clicking / Input text

Clicking button


Send Text


Waits

Implicit Waits

An implicit wait instructs Selenium WebDriver to poll DOM for a certain amount of time, this time can be specified, when trying to find an element or elements that are not available immediately.


Hp procurve dhcp snooping. Explicit Waits

Explicit wait make the webdriver wait until certain conditions are fulfilled . Example of a wait


List of explicit waits

  • alertIsPresent()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • elementToBeSelected()
  • frameToBeAvaliableAndSwitchToIt()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • presenceOfAllElementsLocatedBy()
  • presenceOfElementLocated()
  • textToBePresentInElement()
  • textToBePresentInElementLocated()
  • textToBePresentInElementValue()
  • titleIs()
  • titleContains()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()



Loading a list of elements like li and selecting one of the element



Read Attribute


Get CSS

CSS values varies on different browser, you may not get same values for all the browser.


Capture Screenshot

This will saved the file as in the path of destFile.


isSelected()

isSelected() method in selenium verifies if an element (such as checkbox) is selected or not. isSelected() method returns a boolean.


isDisplayed()

isDisplayed() method in selenium webdriver verifies and returns a boolean based on the state of the element (such as button) whether it is displayed or not.


isEnabled()

is_enabled() method in selenium python verifies and returns a boolean based on the state of the element (such as button) whether it is enabled or not.

Selenium Python Cheat Sheet Examples

Minimum modules to import


Selenium Cheat Sheet Pdf

Created : 17 December 2019





Coments are closed