Recently, the internet has become huge and this is all because of the development of technologies that make it more user-friendly. Furthermore, processes such as developing and testing web applications have become increasingly automated.
Tools such as Playwright are really important when using the web. It helps us save time by quickly opening our apps on a browser, as well as do things like click on stuff, type text and collect public data from the internet. This article is about how we can use Playwright for automation and even web scraping.
Automation Just Got Easier with Playwright
Playwright is a cool tool that helps you to make web browser interactions simpler. It lets you write code which opens browsers and allows you do so many things like going to websites, typing words, pushing buttons, or grabbing text. The best part is that Playwright supports multiple pages at the same time without getting stuck or having any delays.
Playwright is great for cross-browser web automation, meaning the same code will work on multiple browsers like Google Chrome, Microsoft Edge using Chromium, Firefox and Safari when using WebKit. It also supports different programming languages such as Node.js, Python, Java, and .NET so you can write codes that open websites and interact with them in any of these languages.
Playwright’s documentation is really comprehensive. It explains all the topics from how to begin working with it, to all the different types of classes and methods.
“Quick and Easy Guide to Using Playwright in Node
Wondering how to use Playwright? No worries, you can use it with Node.js and Python!
If you’re using Node.js, create a new project and install the Playwright library. You just need two command lines:
An example of a basic code to open up a custom page looks like this:
The first line of code loads Playwright. Then, different web browsers such as Chromium, Firefox, and Webkit open up. Next, the page opens on the Amazon website followed by a 1-second wait for it to show up appropriately. Finally, the browser closes at the end.
It is quite easy to write code in Python. To do that, you need to install Playwright, a special library that works with Python. First, type a command called ‘pip’ into your computer which will allow the installation of Playwright to start. Once you’ve done this correctly, then add some extra features so you can use the right kind of browsers.
Playwright has two different ways it can work – synchronous and asynchronous. An example of this is seen below with asynchronous being used as an example:
This code looks similar to Node.js code but there is one main difference – instead of using camelCase for the function names, we use snake_case. We also launch an instance of Chrome when we use this code, and if you want to launch it in headless mode then you add True to the end.
If you use Node.js, you can have more control and create multiple browser windows in one go by using a context object. This will open the pages in separate tabs. In addition to this, you can get the browser window that each page belongs to by using the page.context() function.
Searching the Web with CSS and XPath – A Practical Example
To interact with an element or extract information from it, the first step is to locate it. Playwright helps you can find elements on a webpage, and you can do this by using something called CSS and XPath selectors.
Let’s look at an example that will help explain this better. Open this link: https://www.amazon.com/b?node=17938598011
When the page has loaded, you’ll see that all the items are inside a category called International Best Seller, which is created by two type of elements called div and class names a-section and a-spacing-base.