- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to refresh a browser then navigate to a new page with Javascript executor in Selenium with python?
We can refresh a page and then navigate to a new page from the current page with Javascript executor in Selenium. Javascript is a language used for scripting and runs on the client side (on the browser). Selenium gives default methods to work with Javascript.
Syntax
driver.execute_script('history.go[0]') javaS = "window.location = 'https://www.tutorialspoint.com/index.htm'" driver. execute_script(javaS)
There are couple of methods of working with Javascript −
Javascript execution at document root level.
In this process, we shall identify the element with locators (class or id) and then perform the required action on it. Then execute_script() method is called and the Javascript is passed as a string to it.
Syntax
javas = "document.getElementsByName('user-search')[0].click();" driver.execute_script(javas)
Please note, we have used getElementsByName('user-search')[0]. The functions like getElementsByName and getElementsById return an array of matching elements. So for locating the first element, the index[0] is used. However if we are using getElementById function, we need not use the index since only one matching element is referred there.
Finally, for execution the web driver will put the Javascript statement in the browser and then perform the necessary action like clicking the required button. This Javascript does not mingle with the Javascript present in the webpage.
Javascript execution at element level.
In this process, we shall identify the element with the help of web driver methods like find_element_by_xpath or find_element_by_id and so on. Then execute the necessary action on it like clicking the element. Finally, the method execute_script() is called. This method has the Javascript statement and the identified webelement as arguments.
Syntax
userN= driver.find_element_by_id("user-search']") driver.execute_script("arguments[0].click();", userN)
The Javascript executor is also capable of returning values. Thus execute_script() can return values for example we can fetch the title of the page using this concept.
Syntax
print driver.execute_script('return document.title')
Example
Code Implementation with Javascript for page refresh and launch a new URL.
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke #actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #to refresh the browser with Javascript executor driver.execute_script('history.go[0]') # to navigate to a new page javaS = "window.location = 'https://www.tutorialspoint.com/index.htm'" driver. execute_script(javaS) #to close the browser driver.close()
- Related Articles
- How to perform back and refresh in a browser in Selenium with python?
- How to click on a button with Javascript executor in Selenium with python?
- How to click on a link with a Javascript executor in Selenium with python?
- How to perform vertical scrolling of a webpage with Javascript executor in Selenium with python?
- How to get the inner text of a webpage with a Javascript executor in Selenium with python?
- How to get the title and URL of a webpage with Javascript executor in Selenium with python?
- How to Use WebDriver Javascript Executor to Navigate to a URL using Postman?
- How to key in values in input text box with Javascript executor in Selenium with python?
- How to close a browser session in Selenium with python?
- How to check a checkbox in a page in Selenium with python?
- How to navigate back to current page from Frame in selenium webdriver?
- How to select a radio button in a page in Selenium with python?
- How to refresh a page in Firefox?
- How to invoke the Chrome browser in Selenium with python?
- How to invoke the Firefox browser in Selenium with python?
