Search This Blog

Saturday, January 8, 2022

Execute Selenium in Jupyter Notebook

Installing and running selenium is so simple with Jupyter Notebook. Follow the below steps to create a sample program. Creating POCs will be superfast with this method. Steps to run Selenium with Python on Jupyter notebook below:

Install Selenium in Jupyter

!pip install selenium

If its successful, you will get the below message

Install Chrome Webdriver

Install from this link: https://chromedriver.chromium.org/

Make sure that you are downloading the version that matches with your chrome browser version. 

Run the sample program below

Intent of this code sample is to search for a person in Google and retreive the Wiki text displayed on the right side of the page. 

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()  
driver.get('https://google.com')
driver.find_element("name", "q").send_keys("Elon Musk")  
driver.find_element("xpath","/html/body/div[1]/div[3]/form/div[1]/div[1]/div[4]/center/input[1]").send_keys(Keys.ENTER) 
print(driver.find_element(By.XPATH,"/html/body/div[7]/div/div[11]/div[3]/div[2]/div/div/div[2]/div/div/div/div[1]/div/div/div/div/span[1]").text)

sleep(3)
driver.close()

Run the code and you will get the below result. 

No comments:

Post a Comment