From the course: Playwright Python and Pytest for Web Automation Testing: Master Modern Web Testing with Playwright and Pytest in Python

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Evaluate JavaScript

Evaluate JavaScript

In this lesson, we will learn how to execute custom JavaScript code in our Python script. For that, move on to your browser and open the developer tools. In the same, open the console like this. If you can see it on the tabs, then look for the same in the more tabs. Now when you have opened the console, you will find a prompt like this where you can write JavaScript code and run the same on our website. Let's say what we want to do is scroll to the end of the page. To do that, we can use the window objects, scroll by method. To the same we can provide the X and the Y values. That is how much to scroll in the x-axis that is horizontally and how much to scroll in the y-axis that is vertically. So for x-axis we don't want to scroll any pixel but for the y-axis let's say we will scroll by 400 pixels. If I hit enter you can see we evaluate the expression and scroll by 400 pixels. Now to scroll to the end of the page that is here, what we can do is use the document that is our HTML…

Contents