Solutions to 5 Common WebDriver Doubts

Common WebDriver Problems And Solutions

We often face issues or doubts while automating using WebDriver some of which can be rectified by our self and other may require help from our colleagues or any online portals. Here is a consolidated list of common WebDriver doubts and their solutions.

Get Element by XPath within JavaScript

Usually elements in javascript are obtained by id, tag name etc. But in most cases, we would want to obtain the element using xpaths values. This can be done using document.evaluate method of javascript. Refer here for more details on it.

You can use the below javascript code to get elements using XPath. This javascript code can be executed using WebDriver’s Javascript executor to act on the element you need.

How to take partial screenshots using Selenium WebDriver

For taking screenshot using WebDriver, we have to cast the webdriver object to TakesScreenShot and use the getScreenshotAs method. The code snippet is shown below for taking full page screenshot. You can use the OutputType argument of getScreenshotAs method to output the screenshot to file, raw bytes(OutputType.BYTES) or as base64 data(OutputType.BASE64).

For taking partial screenshot, we have to get the screenshot File object as above and get the partial image using ImageIO (javax.imageio.ImageIO) as shown below.

Wait Till element is present

We can use WebDriverWait in cases where we have to wait till an element is visible or comes to an expected state. You can refer this post for more details.

SEE ALSO:   How to Handle Alerts and Frames in WebDriver

How to make FirefoxDriver use existing profiles

Often we may want to make the driver use existing profiles to test using certain plugins or data etc. This can be done in firefox by providing the path to the profile folder as shown below.

Custom data folder for chrome

How to Perform File Upload and Download using WebDriver

We have covered these topics before and you can refer this post for file upload and this for file download using webdriver.

We will be covering more Issues in WebDriver in future posts.Subscribe to our newsletter or follow us on Facebook or Twitter to get the latest updates

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *