Locating Elemetens in Selenium
Certainly! This script is a basic example of using Selenium with Python to automate web browsing, specifically for interacting with the Python.org website. Let’s break it down step by step:
-
Import Statements:
- This imports the necessary modules from the Selenium package:
webdriver
is used to control the browser.Keys
contains special keys for keyboard interaction.By
provides methods to locate items on a page (like by name, ID, etc.).
- This imports the necessary modules from the Selenium package:
-
Launching the Browser:
- This line creates an instance of the Firefox WebDriver. It will open a Firefox browser window when executed.
-
Navigating to a Website:
- This command tells the WebDriver to navigate to the specified URL, in this case,
http://www.python.org
.
- This command tells the WebDriver to navigate to the specified URL, in this case,
-
Checking the Page Title:
- This line is a simple check (assertion) to confirm that the title of the page contains the word “Python”. It’s a way to ensure that the page has loaded correctly and is indeed the Python homepage.
-
Finding an Element:
- This finds an element on the page with the name attribute “q” (which is the search box on the Python website).
-
Interacting with the Element:
- This clears any pre-existing text from the search box.
- This simulates typing “pycon” into the search box.
- This simulates pressing the Enter key, which submits the search.
-
Checking Page Contents:
- After searching, this line checks that the phrase “No results found.” is not present in the page source, indicating that the search yielded results.
-
Closing the Browser:
- Finally, this line closes the browser window.
This script demonstrates basic Selenium operations like opening a website, finding elements, interacting with them (like entering text and submitting forms), and performing simple checks. It’s a foundational script for web automation and testing.
Sure, let’s break down this Python script which is using the Selenium library along with the unittest
framework for testing. The script is structured to test the functionality of searching on the Python.org website.
-
Import Statements:
unittest
: This module is Python’s standard library for writing and running tests.webdriver
: This is part of Selenium, used to control the browser.Keys
: A Selenium module to simulate keyboard keys.By
: Used to locate elements within a page.
-
Class Definition:
- This line defines a test case class
PythonOrgSearch
that inherits fromunittest.TestCase
. This is the base class for all tests inunittest
.
- This line defines a test case class
-
setUp Method:
setUp
is a special method inunittest
. It is called before each test method starts. It’s typically used to set up the environment before running the tests.self.driver = webdriver.Firefox()
: This line initializes the Firefox WebDriver and opens the Firefox browser. Theself.driver
variable is used to refer to the browser throughout the class.
-
Test Method:
- This method, named
test_search_in_python_org
, is the actual test case. Inunittest
, any method that starts withtest
is considered a test case. - Inside this method, various steps are performed to conduct the test:
driver.get("http://www.python.org")
: Opens the Python.org website.self.assertIn("Python", driver.title)
: Checks if “Python” is in the page title.elem = driver.find_element(By.NAME, "q")
: Finds the search box element.elem.send_keys("pycon")
: Types “pycon” in the search box.elem.send_keys(Keys.RETURN)
: Simulates pressing the Enter key to submit the search.self.assertNotIn("No results found.", driver.page_source)
: Ensures “No results found.” is not present in the page source.
- This method, named
-
tearDown Method:
tearDown
is another special method inunittest
, called after each test method completes.self.driver.close()
: Closes the browser window.
-
Running the Test:
- This conditional checks if the script is being run directly (as opposed to being imported as a module). If it is, it calls
unittest.main()
, which runs the test case (i.e., thePythonOrgSearch
class).
- This conditional checks if the script is being run directly (as opposed to being imported as a module). If it is, it calls
This script is a basic example of a functional test: it automates a web browser to simulate a user searching for “pycon” on Python.org and checks that the search works as expected. The use of the unittest
framework helps organize the test structure and provides easy-to-understand results.
Drag and Drop
3.6. Navigation: history and location
Earlier, we covered navigating to a page using the “get” command ( driver.get("http://www.example.com")
). As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. To navigate to a page, you can use get method:
driver.get(“http://www.example.com”)
To move backward and forward in your browser’s history:
driver.forward() driver.back()
Please be aware that this functionality depends entirely on the underlying driver. It’s just possible that something unexpected may happen when you call these methods if you’re used to the behavior of one browser over another.
3.7. Cookies
Before moving to the next section of the tutorial, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for:
Go to the correct domain
driver.get(“http://www.example.com”)
Now set the cookie. This one’s valid for the entire domain
cookie = {‘name’ : ‘foo’, ‘value’ : ‘bar’} driver.add_cookie(cookie)
And now output all the available cookies for the current URL
driver.get_cookies()