This is our third article in Python Selenium, in this article we are going to learn about Selenium Web Driver Navigation Command in Python. so using Selenium Navigation Command we are able to open a web page URL, navigate to other web page by clicking any element, Back, Forward and Refresh web page using browser’s history. also you can read the previous articles about Python Selenium in the below links.
Python Selenium Articles
1: Python Selenium Introduction & Installation
2: Python Selenium Web Elements Introduction
So now this is the complete code for Python Selenium Web Driver Navigation Command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
from selenium import webdriver import time #open chrome driver = webdriver.Chrome() #open url driver.get("https://www.google.com") #find element search_box = driver.find_element_by_name("q") #send keys search_box.send_keys("Python") time.sleep(5) #submit the button search_box.submit() #navigate back driver.back() print(driver.title) time.sleep(3) #navigate forward driver.forward() driver.refresh() |
In this code we have opened a browser, basically we are using Chrome browser, but you can use another browsers also. we have already talked that how you can add Python Selenium Web Drivers for Chrome And FireFox browsers Python Selenium Introduction & Installation .
1 |
driver = webdriver.Chrome() |
So now we are going to open Google search page using this code
1 |
driver.get("https://www.google.com") |
In here we are going to find the search text input name, because we want to interact with google search text field, basically after opening of the google website, we want to write Python keyword and search that keyword. you can read the article about web elements in python selenium Python Selenium Web Elements Introduction.
1 2 3 4 |
search_box = driver.find_element_by_name("q") search_box.send_keys("Python") time.sleep(5) search_box.submit() |
You can use this code for navigating back, forward and refreshing.
1 2 3 |
driver.back() driver.forward() driver.refresh() |
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email