Selenium Navigation - Using Javascript
Navigations in selenium can be done in different ways. In this tutorial, we will invoke a script to navigate to a different page.
But before that, please make sure you have read the
first blog on this
series to do the prerequisites.
Selenium Navigation Executing a Script
- Create a file seleniumnavscript.py and paste the following codes
from selenium import webdriver import time
The codes above imports the required library that we will use. - Add this line
driver = webdriver.Firefox(executable_path="geckodriver.exe")
The code above will create a webdriver instance for Firefox. - Add this line
driver.execute_script("window.location.href='https://slackingslacker.github.io/seleniumindex#/about';")
The line will execute a script to go to the webpage https://slackingslacker.github.io/seleniumindex#/about. - Add this line
time.sleep(10)
We will pause the program for 10 seconds so that you can see that the page loaded. - Add this line
driver.close()
The line will close the webdriver as well as the browser. - Run the seleniumnavscript.py. It should do the following:
- Open the firefox browser
- Execute a script to navigate to a page
- Halts for 10 seconds
- Closes the browser
Final Selenium Code
from selenium import webdriver import time driver = webdriver.Firefox(executable_path="geckodriver.exe") driver.execute_script("window.location.href='https://slackingslacker.github.io/seleniumindex#/about';") time.sleep(10) driver.close()
No comments:
Post a Comment