selenium in python

Selenium is a powerful tool to automate browser activities such as filling forms, clicking buttons, and downloading files. Here's a basic example of how to use Selenium in Python.

main.py
from selenium import webdriver

# specify the path to the chromedriver executable
driver = webdriver.Chrome('/path/to/chromedriver')

# navigate to a URL
driver.get('https://www.google.com')

# find an element on the page using its name attribute
search_box = driver.find_element_by_name('q')

# type a query into the search box
search_box.send_keys('hello world')

# submit the query (by hitting the Enter key)
search_box.submit()

# wait for the search results to load
driver.implicitly_wait(10)

# print the page title and URL
print(driver.title)
print(driver.current_url)

# close the browser window
driver.quit()
618 chars
27 lines

This code opens a Chrome browser window using the chromedriver executable, navigates to the Google homepage, searches for the phrase "hello world", waits for the search results to load, and prints the title and URL of the resulting page. Finally, it closes the browser window.

gistlibby LogSnag