Skip to content

Instantly share code, notes, and snippets.

View StephenFordham's full-sized avatar

Stephen Fordham StephenFordham

View GitHub Profile
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
search_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[id="orb-search-q"]')))
#search_box = driver.find_element_by_css_selector('input[id="orb-search-q"]')
from selenium.webdriver.chrome.options import Options
headless_options = Options()
headless_options.add_argument('--headless')
driver = webdriver.Chrome(options=headless_options)
@StephenFordham
StephenFordham / iterate_through_headlines.py
Created August 8, 2020 15:18
iterate_through_headlines
top_titles = driver.find_elements_by_css_selector('div[class="css-14rwwjy-Promo ett16tt11"]')
with open('Coronavirus_headlines.txt', 'w') as cor_virus:
for title in top_titles:
headline = title.find_element_by_css_selector('p[class="css-1aofmbn-PromoHeadline ett16tt4"]').text
cor_virus.write('\n')
cor_virus.write(headline)
print(headline)
@StephenFordham
StephenFordham / actionchain_oneliners.py
Created August 8, 2020 15:01
actionchain_oneliners
search_box = driver.find_element_by_css_selector('input[id="orb-search-q"]')
ActionChains(driver).move_to_element(search_box).click()\
.send_keys('Global coronavirus updates').key_down(Keys.ENTER).perform()
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.bbc.co.uk/news')
@StephenFordham
StephenFordham / CoronaVirusHeadlines_class.py
Last active August 7, 2020 14:07
CoronaVirusHeadlines_class
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
class CoronaVirusHeadlines(object):
def __init__(self):
self._driver = webdriver.Chrome()
def get_headlines(self):
# Magic methods through short code snippets
class Restaurants(object):
def __init__(self, wk1):
self._wk1 = wk1
def __setattr__(self, key, value):
if not isinstance(value, list):
raise TypeError('please enter weekly takings a list')
for val in value:
total = wk1_city_1 + wk1_city_2
print(total)
total = wk1_city_1.__add__(wk1_city_2)
print(total)
# Output
# [30, 60, 90, 120, 150]
# [30, 60, 90, 120, 150]
@StephenFordham
StephenFordham / magic_methods_directories.py
Created July 17, 2020 07:52
magic_methods_directories
print(dir(dict))
print(dir(list))
# Output
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append',
class Restaurants(object):
def __init__(self, wk1):
self._wk1 = wk1
def __repr__(self):
return 'Restaurants({})'.format(self._wk1)
wk1_city_1 = Restaurants([10, 20, 30, 40, 50])
print(wk1_city_1)