I went through a course on udemy ( Automate boring stuff with python ) back in 2018, from there I got that we can automate many tasks from daily life.
Selenium is widely used in writing automated tests for web-applications. Selenium lets you run an automated instance of web browser( chrome, firefox ), and gives you APIs so you can control the browser and webpage.
Lets take a simple example of reading an online epaper. Steps to get to last point are,
Doing this could easily take 3-5 minutes. With selenium we can sum it up to a single command.
url = 'https://www.readwhere.com/newspaper/deshonnati/Akola-Main/557?refquery=deshonnati%20akola'
browser = webdriver.Firefox(
executable_path=r'/usr/local/bin/geckodriver')
browser.get(url) # URL of newspaper
browser.maximize_window()
WebDriverWait
we tell browser to wait until the element appears on the screen and is clickable(EC.element_to_be_clickable
).
We search the element by its DOM path called XPath (By.XPATH
), and then perform a click.# Click on "READ NOW"
WebDriverWait(browser, 6).until(EC.element_to_be_clickable(
(By.XPATH, '/html/body/div[4]/div/div/div[3]/div/div[2]/div[1]/div[3]/a'))).click()
# Click on "Skip Sign in"
WebDriverWait(browser, 6).until(EC.element_to_be_clickable(
(By.XPATH, '//*[@id="skip-area-id"]'))).click()
# Click on "Zoom + button"
WebDriverWait(browser, 6).until(EC.element_to_be_clickable(
(By.XPATH, '/html/body/div[7]/div[1]/div[1]/div[4]/div/button[1]'))).click()
inspect element
.copy
, then select Copy XPath
options = Options()
options.headless = True
browser = webdriver.Firefox(options=options,
executable_path=r'/usr/local/bin/geckodriver')
Find the complete code here
Some other ideas you can try out.
sendkeys()
can be used for that). Make sure you are not publishing these username and password in public repos.