Execute JavaScript

In Previous section, we have discussed about the waits in Selenium Python. Now we will learn about the Execution of Java Script in Selenium Python. It is very important topic and simple. If you know how to Execute JavaScript in Selenium Java then it will be very easy for you to understand this concept in Selenium Python.

Objective:

  • Introduction to JavaScript Executor
  • Introduction to JavaScript
  • Execute JavaScript in Selenium

Introduction to Javascript Executor:

JavaScript Executor is a utility which is kind of API available in Selenium Python to Execute JavaScript. It helps programmer to run the JavaScript code inside your selenium. There are lots of features are not available is Selenium directly which can be achieved through JavaScript by the help of javascriptexecutor like scroll down, scroll up or if you want to click on a link through the JavaScript where normal click is not working. If you want to generate some Alert in webpage through selenium then you can do that through Javascriptexecutor.

What is JavaScript and how it will be applicable to Website?

Let’s have a look with one practical example. Go to https://www.amazon.com/ website and inspect. Go to Console and clear all console.

Execute JavaScript

Now type below line in console Tab.

alert(“Welcome to TestingLPoint”) and Enter

Once you enter, you can see immediately a alert popup in website. This is called javaScript. Now let’s have a look another example.

Go to https://www.testinglpoint.com/contact-us/ website and go to console page type below line and Enter. Here our moto is to send the text to the user name input field which we are doing through driver.find_element(By.ID, ‘Id of the element’).sendKeys(“TesingLPoint”)

Test Scenario:

Let’s think a scenario where our normal click() is not working and we need to click on that link at any cost. In that case, you can use JavaScript to click the particular link.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.amazon.in/')

best_sellers = driver.find_element(By.LINK_TEXT, 'Best Sellers')
driver.execute_script("arguments[0].click();", best_sellers)

Output:

Now you can see from the above example you can click the link through the java script code also. For this you need to use below code.

best_sellers = driver.find_element(By.LINK_TEXT, 'Best Sellers')
driver.execute_script("arguments[0].click();", best_sellers)

Like this if you want to print Page title through the Javascript then you can use below code to achieve that.

title = driver.execute_script("return document.title;")
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.amazon.in/')

best_sellers = driver.find_element(By.LINK_TEXT, 'Best Sellers')
driver.execute_script("arguments[0].click();", best_sellers)

title = driver.execute_script("return document.title;")
print(title)

Q. Can we  refresh the page through JavaScript Executor?

Yes, we can refresh the page through the JavaScript Executor in Selenium Python. For this you need to run below line.

driver.execute_script("history.go(0);")

Now Let’s think of an advance scenario. Like printing page title, clicking on a link we can also generate alert popup through the javaScript executor.

from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.amazon.in/')

best_sellers = driver.find_element(By.LINK_TEXT, 'Best Sellers')
driver.execute_script("arguments[0].click();", best_sellers)

title = driver.execute_script("return document.title;")
driver.execute_script("history.go(0);")
driver.execute_script("alert('TestingLPoint');")

Output:

Note: You need to put “;” to the end of your javascript code inside the execute_script() method.

Test Scenario:

If you want to verify the content of the webpage then what you will do? You can do the verify with the Page source. But in page source with your content, all html tag, script etc. will be shown which is not our requirement. With the help of Java script  executor we can extract all content of the page and can show in the console. For this we need to use below code.

inner_text = driver.execute_script("return document.documentElement.innerText;")
print(inner_text)
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.amazon.in/')

best_sellers = driver.find_element(By.LINK_TEXT, 'Best Sellers')
driver.execute_script("arguments[0].click();", best_sellers)

title = driver.execute_script("return document.title;")

inner_text = driver.execute_script("return document.documentElement.innerText;")
print(inner_text)

Output:

Test Scenario:

There is another scenario where we are taking a screenshot of the page for bug and mark some regions to showcase and attach to the bug ticket. Can we do this whole process in Selenium Python.

Yes, we can do this with the help of Javascript Executor. With the java script executor we can apply the stylesheet (CSS) and after that we can take Screenshot. For taking screenshot we have discussed in earlier section. You can visit “Take Screenshot” tutorial for leaning more.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.amazon.in/')

best_sellers = driver.find_element(By.LINK_TEXT, 'Best Sellers')
#driver.execute_script("arguments[0].click();", best_sellers)

title = driver.execute_script("return document.title;")
#driver.execute_script("history.go(0);")
#driver.execute_script("alert('TestingLPoint');")

inner_text = driver.execute_script("return document.documentElement.innerText;")
print(inner_text)
print(title)

driver.execute_script("arguments[0].style.border = '3px solid red'", best_sellers)

Output:

Scroll Down and Scroll Up:

Scroll Down and Scroll Up is very important scenarios which you are doing most of the time in manual testing. If you want to automate this scenario, then you can do with the help of Javascript Executor. Below code will help you to scroll down to end of the page.

Example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.amazon.in/')

driver.execute_script("arguments[0].style.border = '3px solid red'", best_sellers)

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

Output:

Like Scroll down, if you want to scroll up to the top of the page then you just need to the interchange the argument inside the scrollTo() like below.  Here we are adding sleep() of 10 sec to observer the scroll down and scroll Up otherwise it will be very fast and we will not able to observe the scroll down and scroll up.

driver.execute_script("window.scrollTo(document.body.scrollHeight, 0);")
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager
import time


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.amazon.in/')


driver.execute_script("arguments[0].style.border = '3px solid red'", best_sellers)

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(10)

driver.execute_script("window.scrollTo(document.body.scrollHeight, 0);")

If you want to scroll down till some element then you need to use below statement.

link_check = driver.find_element(By.LINK_TEXT, 'See all deals')
driver.execute_script("arguments[0].scrollIntoView(true);", link_check)
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager
import time


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.amazon.in/')

best_sellers = driver.find_element(By.LINK_TEXT, 'Best Sellers')
#driver.execute_script("arguments[0].click();", best_sellers)

title = driver.execute_script("return document.title;")
#driver.execute_script("history.go(0);")
#driver.execute_script("alert('TestingLPoint');")

inner_text = driver.execute_script("return document.documentElement.innerText;")
print(inner_text)
print(title)

driver.execute_script("arguments[0].style.border = '3px solid red'", best_sellers)

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)

driver.execute_script("window.scrollTo(document.body.scrollHeight, 0);")
link_check = driver.find_element(By.LINK_TEXT, 'See all deals')
driver.execute_script("arguments[0].scrollIntoView(true);", link_check)

If you want to use sendKeys by the help of JavaScript Executor then you can use below code to perform the same task.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager
import time


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://www.facebook.com/')
email = driver.find_element(By.ID, 'email')
password = driver.find_element(By.ID, 'pass')

driver.execute_script("document.getElementById('email').value = 'TestingLPoint';")

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *