Selenium is a popular open-source tool that automates web browsers and is commonly used for testing web applications. In this article, we will use the Firefox driver to navigate to the YouTube home page, locate the search bar, and enter a search query.
Prerequisites
- Python 3.10
- Selenium library
- Firefox browser
Setup
Install the Selenium library using pip:
pip install selenium
Steps
-
Import the necessary libraries
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By
-
Create a new instance of the Firefox driver
driver = webdriver.Firefox()
-
Navigate to the YouTube home page
driver.get("https://www.youtube.com/")
-
Locate the search bar using xpath
'//*[@id="search"]'
search_bar = driver.find_element(By.XPATH, '//*[@id="search"]')
-
Enter a search query
search_bar.send_keys("《想我就打给我》-Bell玲惠")
-
Press the Enter key to submit the search query
search_bar.submit()
-
(Optional) Close the browser
driver.close()
By following these steps, you can automate a search on the YouTube website using the Selenium Python module. You can modify the code to search for anything you like, and it can be used as a foundation for more complex automation tasks. The full code can be found here.
Top comments (0)