Skip to content

Instantly share code, notes, and snippets.

@yogendratamang48
Created April 24, 2020 09:17
Show Gist options
  • Save yogendratamang48/f458ffe83326a2745ff2393c0438f91c to your computer and use it in GitHub Desktop.
Save yogendratamang48/f458ffe83326a2745ff2393c0438f91c to your computer and use it in GitHub Desktop.
Selenium plus some generic functions
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
import logging
from datetime import datetime
import csv
import os
import json
if not os.path.exists("data"):
os.mkdir("data")
class Utils:
@staticmethod
def setup_selenium_driver():
logging.info("starting init selenium")
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
selenium_driver = webdriver.Chrome(chrome_options=chrome_options)
logging.info("finished init selenium")
return selenium_driver
@staticmethod
def setup_selenium_firefox():
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path="/usr/local/bin/geckodriver")
logging.info("starting init selenium")
return driver
@staticmethod
def write_csv(filename, data):
""" appends data """
with open(filename, "a", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
writer.writerow(data)
@staticmethod
def init_csv_file(filename, header):
"""
initializes a file with header
"""
with open(filename, mode="w", newline="", encoding="utf-8") as cp:
writer = csv.writer(cp)
writer.writerow(header)
@staticmethod
def json_to_unicode(filename):
jdata = json.loads(open(filename).read())
with open(filename, "w") as fp:
json.dump(jdata, fp, indent=2, ensure_ascii=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment