Sunday, May 31, 2020

Web Scraping Using Selenium - Navigation with Anchor Element

Selenium Navigation - Anchor Navigation

Navigations in selenium can be done in different ways. In this tutorial, we will use the click functionality of an anchor tag.
But before that, please make sure you have read the first blog on this series to do the prerequisites.

Selenium Navigation Using Anchor Element

  1. Create a file seleniumnavachor.py and paste the following codes
    from selenium import webdriver
    import time
    
    The codes above imports the required library that we will use.
  2. Add this line
    driver = webdriver.Firefox(executable_path="geckodriver.exe")
    
    The code above will create a webdriver instance for Firefox.
  3. Add this line
    driver.get("https://slackingslacker.github.io/seleniumindex")
    
    The line will got to the website (https://slackingslacker.github.io/seleniumindex).
  4. Add this line
    time.sleep(5)
    
    We will pause the program for 5 seconds.
  5. Add this line
    el = driver.find_element_by_css_selector("div[class='navbar-start'] > a:last-of-type")
    
    This code will find an element using a CSS selector. We will tackle this more in the future. The element that we are looking for is the About link in the Menu Bar at the top
  6. Add this line
    el.click()
    
    This will click the About in the menu.
  7. Add this line
    time.sleep(10)
    
    We will pause the program for 10 seconds to see that it loaded the about page.
  8. Add this line
    driver.close()
    
    The line will close the webdriver as well as the browser.
  9. Run the seleniumnavachor.py. It should do the following:
    • Open the firefox browser
    • Browser goes to https://slackingslacker.github.io/seleniumindex
    • Halts for 5 seconds
    • Finds the anchor tag for the About in the top menu
    • Clicks the link
    • Halts for 10 seconds
    • Closes the browser
 

Final Selenium Code

from selenium import webdriver
import time

driver = webdriver.Firefox(executable_path="geckodriver.exe")
driver.get("https://slackingslacker.github.io/seleniumindex")
time.sleep(5)

el = driver.find_element_by_css_selector("div[class='navbar-start'] > a:last-of-type")
el.click()
time.sleep(10)

driver.close()

 

Conclusion

Navigation in selenium can be done using an anchor element.
 

No comments:

Post a Comment

Programming

Basic Web Scraping Using Python - A Beginner's Guide to using Requests and Selenium

Beginner Guide to Web Scraping Using Python For Requests and Selenium (Live Examples)   Web scraping is gathering da...