Last active
July 28, 2020 07:07
-
-
Save suriyadeepan/b7cf224d47631550d86f9eba402fe199 to your computer and use it in GitHub Desktop.
Autonomous surfer for chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
import time | |
browser=webdriver.Chrome() | |
#first tab | |
browser.execute_script("window.open('about:blank', 'tab1');") | |
browser.switch_to_window("tab1") | |
browser.get('http:/reddit.com') | |
time.sleep(5) | |
#second tab | |
browser.execute_script("window.open('about:blank', 'tab2');") | |
browser.switch_to.window("tab2") | |
browser.get('http://bing.com') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
import time | |
import csv | |
import os | |
import sys | |
def init_driver(tabs): | |
driver = webdriver.Chrome() | |
driver.get(tabs[0]) | |
return driver | |
def new_tab(driver, url): | |
driver.execute_script('''window.open("{}","_blank");'''.format(url)) | |
def read_conf_from_file(filename): | |
with open(filename, 'r') as csv_file: | |
reader = csv.reader(csv_file, delimiter=' ') | |
tabs, durations = [], [] | |
for row in reader: | |
tabs.append(row[0]) | |
durations.append(int(row[1])) | |
return tabs, durations | |
if __name__ == '__main__': | |
u_conf_file = input('Enter config file name :: ') # get conf file input from user | |
# check if config file exists | |
if not os.path.exists(u_conf_file): | |
sys.exit('Bad config file!') | |
tabs, durations = read_conf_from_file(u_conf_file) # read from conf file | |
u_frequency = int( | |
input('Enter frequency of autmation (seconds) :: ')) # get frequency of automation | |
# init driver | |
driver = init_driver(tabs) | |
for tab in tabs[1:]: | |
new_tab(driver, tab) | |
# automation loop | |
while True: | |
for handle, duration in zip(driver.window_handles, durations): | |
driver.switch_to.window(handle) | |
driver.execute_script("window.scrollTo(0, 1000)") | |
time.sleep(duration) | |
driver.execute_script("window.scrollTo(0, 0)") | |
# go to sleep for `u_frequency` seconds | |
time.sleep(max(1, # we don't wanna deal with negative values here | |
u_frequency - sum(durations) - 10*len(durations)) ) # assuming it takes ~10 seconds for a webpage to load |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment