DEV Community

Discussion on: Writing Test Cases using Python with Selenium

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao • Edited

Hmmm... for me I choose XPath because it's much easier to read and modify which using CSS fails me in extracting data.

As there were times I need to extract data and makes changes to each value in the rows of a table then save it as an element in either a dictionary or list data type variable.

Especially you are extracting listings data. Which you can program your Data Scrapper to stop at specific rows using just for loop.

After getting the total number of rows from somewhere within the page or derived from checking the usual max number of rows in the table html element.

Collapse
 
endtest_io profile image
endtest

XPaths do have an advantage, you can use them to locate an element based on the text inside.

//*[contains(text(),'Hello')]

It's great to use both CSS Selectors and XPaths.

And it's also very helpful to learn how to write your own XPaths and CSS Selectors by using attributes which make an element unique.

The XPaths and CSS Selectors that you get from the Chrome Developer Console.

Let's take a look at this example:

<html>
   <body>
      <div class="login-form">
         <input id="mat-input-6" type="text" placeholder="Email">
         <input id="mat-input-9" type="password" placeholder="Password">
      </div>
   </body>
</html>

We can easily spot that the IDs are dynamic, so we have to avoid them.

The basic CSS Selector for the Email input would look like this:

body > div > input[type=text]:nth-child(1)

But we can easily construct something more reliable:

input[placeholder="Email"]

And using Cypress or any other JavaScript-based UI testing library is a very bad idea.

Since those libraries have severe limitations and do not mimic real user behavior.

For example, a JavaScript-based library for testing the UI can easily click on an element which is completely covered by another element.

A real user and a Selenium test cannot do that.

A JavaScript library for testing the UI cannot perform file uploads, cannot manage iframes and cannot manage multiple browser tabs.

Use Selenium or some other Selenium-based solution. That's the way to go.

Thread Thread
 
steelwolf180 profile image
Max Ong Zong Bao

Interesting perspective as I had used protractor and did find that it was quite limited in features to test UI which I usually fall back to using selenium due to me playing around with it in the past by doing data scrapping projects.