Select Page

Category Selected: Selenium Testing

47 results Found


People also read

Software Development

CSS Tips and Tricks for Responsive Design

Software Tetsing

Changing dimensions in a data warehouse: How to Test

Artificial Intelligence

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility
XPath Examples for HTML Table

XPath Examples for HTML Table

Every automation tester should possess the following two skills while developing an automation test script. 1) Getting to know automation tools 2) locating web elements. As an Selenium Testing Services company, we have mitigated several failed Selenium automation testing projects. Most of them failed due to not following test automation best practices, writing absolute XPaths & CSS Selectors. Learning Selenium Tool commands is easy. However, writing robust Selenium locators comes from experience. To help newbie test automation engineers, we have written this blog article with various XPath Examples for HTML Tables.

XPath to locate a table using row count

Sometimes you may need to identify an HTML table using its row count. Using XPath count function, you can find a table using row count. The below table has 6 rows including a row for headers. Let’s see how to identify using its row count.

XPath Examples with Count Function

XPath

//table[count(.//tr)=6]

locate using column count

Locating a table using column count is simple. Just add ‘th’ in the Row count Xpath.

XPath

//table[count(.//tr/th)=4]
  

Locate Last Row

You no need to use For Loop to identify last row in a table. Use ‘Last’ XPath function to locate last row.

XPath Examples with Last Function

XPath

//table[@id='tbl1']//tr[last()]
  

Identify Alternate Rows

XPath Examples with Alternate Rows

CSS

table[id='tbl1'] tr:nth-child(2n+1)
  
How to take a Screenshot in Selenium WebDriver

How to take a Screenshot in Selenium WebDriver

Screenshots are important to analyze the execution failures. It helps an automation tester to analyze what went wrong during execution. As one of the best automation testing services companies, we have also faced several challenges when troubleshooting test automation failures. The screenshot is a great reference document to recheck how the functionalities behaved previously and to identify false negatives from script failures.

In this blog article, we would like to share how to take screenshots in Selenium WebDriver using various commands.

Full Page Screenshot

In Selenium 4, you can take full page screenshot. This feature is very helpful to investigate failures in detail.

File file = ((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE);
  

Note: As of now, full page screenshot works only for Firefox driver.

Element Screenshot

Sometimes you may need screenshot only for a specific element instead of capturing the entire page.

driver.findElement(By.id("txt1")).getScreenshotAs(OutputType.FILE)
  

Full Page Screenshot using aShot

aShot is a WebDriver Screenshot utility. It takes a screenshot of the WebElement on different platforms (i.e. desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser).

Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png"));
  

Viewport Screenshot

File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  
Selenium WebDriver Exceptions

Selenium WebDriver Exceptions

Analysing automation script failures using Exception details is an important skill for an automation tester. Selenium and Appium are the most widely used open-source automation testing tools. If you are aware of the Selenium WebDriver Exceptions list, it would put you at ease while troubleshooting failures. As a leading test automation services company, we would like to list Selenium WebDriver exceptions and their reasons.

Unsupported Command Exception This exception is triggered when the Selenium client library sends a command which is unknown to Selenium Server. For example: If you are using a higher version of the Selenium client library which has new commands, then the lower versions of the Selenium Server can’t understand new commands which are sent from the client.

Stale Element Reference Exception The stale element reference error is a WebDriver error that occurs because the referenced web element is no longer attached to the DOM.

Every DOM element is represented in WebDriver by a unique identifying reference, known as a web element. The web element reference is a UUID used to execute commands targetting specific elements, such as getting an element’s tag name and retrieving a property of an element.

When an element is no longer attached to the DOM, i.e. it has been removed from the document or the document has changed, it is said to be stale. Staleness occurs for example when you have a web element reference and the document it was retrieved from navigates.

Reference: Mozilla MDN Web Docs

Script Timeout Exception This exception is triggered when Selenium JavaScriptExecutor command exceeds its set timeout. The default timeout is 30 seconds. However, you can extend it using Desired Capabilities.

Invalid Selector Exception If XPath/CSS Selector syntax is invalid, then it will throw Invalid Selector Error. Or if the selector’s expression is not selecting an element e.g. count(//input), then Invalid Selector Exception will be invoked.

Invalid Argument Exception The invalid argument error is a WebDriver error that occurs when the arguments passed to a command are either invalid or malformed.

Invalid argument errors can be likened to TypeErrors in JavaScript, in that they can occur for a great many APIs when the input value is not of the expected type or malformed in some way. See the type- and bounds constraints for each WebDriver command.

Selenium WebDriver Window Handling using Python

Selenium WebDriver Window Handling using Python

In this blog article, we have covered all the Selenium WebDriver window handling techniques which are helpful for automation testers. Let’s learn the commands.

Get Window Handles

driver.window_handles
  

Switch To Window

window_handle = driver.window_handles[1]
driver.switch_to.window(window_handle)
  

As per WebDriver W3C specification, you can switch between windows only with window handle. However, you can also write your own method to switch windows using Window name.

Switch To Window By Name

#Method Definition
def switch_window_by_name(webdriver,window_name):
    for handle in webdriver.window_handles:
        webdriver.switch_to.window(handle)

        if webdriver.title == window_name:
            return handle

#Method Call
switch_window_by_name(driver,"Window1")
  

Switch To Window Position

window_handle = driver.window_handles[1]
driver.set_window_position(10,10)
  
Executing JavaScript in Selenium using Python

Executing JavaScript in Selenium using Python

In this blog article, you will learn how to execute JavaScript in Selenium using Python. As a software testing company, writing technical articles which are helpful for newbie automation testers gives us immense happiness.

Before using the snippets, please note that your scripts may fail unexpectedly due to cross-domain policies. Executing JavaScript in Selenium using Python should be your last resort since it does not mimic user actions.

Entering Value in Textbox

txtBox = driver.find_element_by_id("txt1")
driver.execute_script("arguments[0].value=arguments[1]",txtBox,"codoid")
  

Get Value

txtBox = driver.find_element_by_id("txt1")
print driver.execute_script("return arguments[0].value",txtBox)
  

Click Event

link = driver.find_element_by_link_text("Window1")
driver.execute_script("arguments[0].click()",link)
  

Storing and Retrieving Global Variables

driver.execute_script("window.variable1=arguments[0]","codoid")
print driver.execute_script("return window.variable1")
  

Scroll Page

driver.execute_script("window.scrollBy(0,150)")
  

Page Status

print driver.execute_script("document.readyState")
  

If the readystate returns ‘complete’, it confirms that the page and its resources have been loaded and parsed.

Splinter – A Wrapper Library for Selenium with Python

Splinter – A Wrapper Library for Selenium with Python

Splinter is a python library and it has common readable wrapper methods for Selenium and zope.testbrowser frameworks. If you would like to write readable automated tests using Python, then Splinter is a good choice. As an automation testing company, exploring new tools/libraries is one of the important R&D activities. Please subscribe our blog to get the updates. Now, let’s see how to use Splinter.

How to install Splinter?

pip install splinter

Browser Launch What we have noticed with Splinter during Browser launch is the browser is launched very quickly. If you are also noticing the same, please feel free to comment over here.

from splinter import Browser
browser = Browser('chrome')

URL Navigation

browser.visit('https://codoid.com')

Find Element You can use the following methods find_by_css, find_by_xpath, find_by_name, find_by_tag, find_by_value, find_by_text, and find_by_id to locate an element.

browser.find_by_id('menu-item-54').click()

All together

from splinter import Browser

browser = Browser('chrome')

browser.visit('https://codoid.com')

browser.find_by_id('menu-item-54').click()

browser.quit()