Receive a quick introduction to Selenium WebDriver, and learn how to interact with it in Python.
- [Instructor] As a tester, you've probably heard of Selenium. In fact, you may have even used it before. It's a very well-known and powerful tool that's used to automate websites. This course isn't about writing test automation, but the fact is, UI tools can be very effectively used to help you better explore your product. So the first thing we'll need to do is to install the Python Selenium package, which of course is easy to do with pip. So we'll use pip install selenium. And that will go ahead and download and install everything that we need for Selenium.
Once we have that done, we'll need to do one additional step. So we'll need to go to the Selenium Python documentation, and in section 1.3, it has drivers. And for each web browser that we want to use, we're going to have to download and install the drivers. So once the installation of the drivers is complete, we can get started with using it. So we can go to visual studio code, and let's create a file, to create our script with. Selenium example, we'll call it.
.py. And in this file, we can start to import. So let's import from selenium, and let's import web driver. Web driver will allow us to drive the browser. So let's create an instance of that. Driver = webdriver.Chrome, which will load up Chrome for us, and let's use that driver to get a URL. So we'll use http, and let's just use google.com.
Keep it nice and simple. So that will load that URL for us in the browser, but we want to do more than just load a page. We want to actually do something with it. So let's create a variable we'll call element. And let's store that with an element that we find on the page. So we'll use driver.find_element_by_name and the name that we'll use is q, which should return the search element for us from the Google page.
So now we have that element and we can do some stuff with it. So let's use that element and let's send keys to it, and let's send a search term. So we'll just search for test, and that will enter the search, test, into the search bar. But we also need to tell it to execute the search. So to do that, we'll need to simulate hitting the enter key, and we can get some helpful stuff from the Selenium module, so from selenium.webdriver.common.keys, we can import capital K Keys.
And now, we can all element.send_keys again, and this time we will send it. Keys.RETURN, which will simulate hitting enter. So at this point, we're ready to go ahead and try this out. So let's save this file. Let's go over to our command prompt and let's run it. Python SeleniumExample.py. And this should load up Chrome for us and go to Google, enter our search term, and simulate hitting enter.
And there we can see that it all worked out for us. There's a lot more to cover with Selenium, but you can see how easy it is to get started with it to do some simple things for you.
- Driving API calls with scripting
- API authentication
- Parsing API data
- Automating reports with Google APIs
- Using the Google Charts and Sheets APIs
- Preparing test sites with Selenium WebDriver
- Analyzing test data with scripting
- Generating pseudo input data for testing