Uncategorized

Selenium Python – How to Get H1 Text from Any Page

import time
from csv import writer
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
#REQUIRED FOR HEADLESS PART 1
from selenium.webdriver.chrome.options import Options
#REQUIRED FOR HEADLESS
from selenium.webdriver.chrome.service import Service

s = Service('/usr/local/bin/chromedriver')
#driver = webdriver.Chrome(service=s)

#Set some selenium chrome options - Part 2
chromeOptions = Options()
chromeOptions.headless = False

#This is where your chromedriver lives. You download this from google. It needs to be stored locally.
#PATH = "/usr/local/bin/chromedriver"

# Initialize Driver Headless by uncommenting appropriate driver below #part 3
driver = webdriver.Chrome(service=s, options=chromeOptions)
# driver = webdriver.Chrome(PATH)
list_data = []

def initialize_browser():
    driver.get("InsertYOURURL")
    time.sleep(1)
    print("starting_Driver")
    content = driver.find_element(by=By.CLASS_NAME, value="post-title")
    pName = content.text
    print("The h1 Text is: " + pName)

initialize_browser()

driver.quit()