How to use Selenium WebDriver commands in Python

126.8K views
•
April 5, 2022
by
SDET- QA
YouTube video player
How to use Selenium WebDriver commands in Python

TL;DR

The video explains five categories of Selenium WebDriver commands: get commands, conditional commands, browser commands, navigational commands, and wait commands. It also shows practical interaction with elements like input fields, radio buttons, check boxes, dropdowns, links and images, including how to fetch page title, URL and page source for validations.

Transcript

okay so from today's session onwards we  are going to see how we can work with the   different type of web driver commands  because so far we have seen like how we can identify the elements on the web applications  so once we identify the web elements so the next thing is we need to perform the actions so the  different elements are having differen... Read More

Key Insights

  • The five command categories in Selenium WebDriver are get commands, conditional commands, browser commands, navigational commands, and wait commands.
  • Get commands fetch information about the page such as title, URL, and page source using driver attributes like title, current_url, and page_source.
  • Application specific commands are accessed through the driver object and include title, current_url, and page_source for validations.
  • Page source retrieval helps validate HTML content when needed, though it is less commonly used for standard validations.
  • Interacting with various element types like input boxes, radio buttons, check boxes, dropdowns, and links is covered in parallel with command usage.
  • The practical example demonstrates opening a URL, printing the page title, and verifying the URL matches the expected address.
  • The Python setup involves creating a driver, launching Chrome with a service object, and using driver.get to load the target URL.
  • The session emphasizes printing and validating dynamic data such as title and URL to ensure the correct page is displayed.

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How to fetch the page title using Selenium WebDriver in Python?

To fetch the page title in Selenium with Python, you first launch the browser and navigate to the target URL using driver.get. Then you access the title attribute of the driver, which returns the current web page title. You can print it or store it for validation. This is useful for asserting that the correct page loaded and for creating comparison checks with an expected title.

Q: How can you obtain the current URL of the loaded page?

The current URL can be obtained via driver.current_url after the page has loaded. This command retrieves the exact URL shown in the browser bar, which you can print or compare against an expected address to verify that navigation occurred correctly. It helps validate redirections and navigation flows within automation scripts.

Q: What is the purpose of driver.page_source in Selenium Python?

Driver.page_source returns the complete HTML source of the current page. This is useful when you need to inspect or validate specific HTML content, attributes, or elements that may not be visible through the UI alone. It can be stored in a variable and checked for presence of certain strings or patterns.

Q: What are the main categories of Selenium WebDriver commands described?

The main categories are get commands for retrieving information, conditional commands for decision making, browser commands for browser-level operations, navigational commands for page movement and history, and wait commands for synchronization. Each category contains multiple commands used to perform actions and validations during automation.

Q: How do you interact with different web elements like input fields and check boxes in Selenium Python?

After locating an element (for example by id, class, or other selectors), you can perform actions such as send_keys to type into input fields, click to select check boxes or radio buttons, and select from dropdowns. The transcript shows applying these actions while explaining that interactions are aligned with the command categories and driver methods.

Q: What setup steps are shown for running Selenium with Python in the video?

The setup involves importing the WebDriver module and service, creating a service object with the driver path, initializing the driver with webdriver.Chrome(service=service_object), and then using driver.get to open the target URL. This prepares the Python environment to automate browser actions and run validations on loaded pages.

Q: How does the speaker suggest validating that the correct page loaded?

Validation is done by capturing the actual title and actual URL of the loaded page and comparing them with the expected values. Printing these values helps verify correctness, and comparing them programmatically confirms that navigation and page loads occurred as intended within the automation script.

Q: Why would you use driver.page_source despite it being rarely required?

Page source can be used when you need to validate HTML structure or content not exposed in the rendered UI. It allows you to perform checks on the underlying HTML to confirm the presence or absence of certain tags, scripts, or metadata, which can be essential for some validation scenarios in automation.

Summary & Key Takeaways

  • The video introduces the five command categories in Selenium WebDriver and demonstrates practical usage for identifying and interacting with web elements. It emphasizes applying get, conditional, browser, navigational, and wait commands to perform actions and validations on web pages.

  • Practical examples include accessing the page title, current URL, and page source, using the driver object, and printing results to verify page state during automation scripts.

  • The session walks through creating a Python setup for Selenium, launching a browser, loading a URL, and performing basic validations with the title, URL, and page content to ensure correct page loading.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from SDET- QA 📚