class Selenium_webdriver: def __init__(self, url_address, type="Chrome"): """ Return chrome webdriver and open the initial webpage. """ from webdriverwrapper import Chrome from selenium import webdriver options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) self.driver = Chrome(options=options) # Open a website window_before = self.driver.window_handles[0] self.driver.get(url_address) def get_site_elements(self): return None def click(self, current_element): #self.driver.find_element_by_xpath() element = self.driver.find_element_by_name(current_element) # the function find_element_by_name should be implemented element.click() def enter(self, current_element, data): element = self.driver.find_element_by_name(current_element) enter_field = self.driver.find_element_by_xpath( "//input[@name='{}']".format(element)) enter_field.clear() data = generate_data() enter_field.send_keys(data)
def setup_chromedriver(mode): # configuring chromedriver: # my screen res: 1366*768: # activate for verbose logging: # service_log_path = "chromedriver.log" # service_args = ['--verbose'] chrome_options = Options() if mode is 'primary': chrome_options.add_argument("--restore-last-session") chrome_options.add_argument("user-data-dir=master_profile") chrome_options.add_argument("--window-size={},{}".format(1000, 800)) chrome_options.add_argument("--window-position={},{}".format(1, 1)) elif mode is 'tertiary': chrome_options.add_argument("--window-size={},{}".format(200, 800)) chrome_options.add_argument("--window-position={},{}".format(1050, 1)) # get rid off: --ignore-certificate-errors command-line flag warning: chrome_options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"]) chrome_options.add_argument("--disable-infobars") # add command line flags chrome = Chrome(chrome_options=chrome_options, # service_args=service_args, # service_log_path=service_log_path ) # log in if not already: if 'readcube.com/session/new' in chrome.current_url and mode is 'primary': chrome.get('https://app.readcube.com/#/library/collection/~/items') # logging in via email / password for i in range(5): try: sleep(3) chrome.find_element_by_id('email').send_keys(user) chrome.find_element_by_id('password').send_keys(pw) chrome.find_element_by_class_name('submit-button').click() except Exception as e: sleep(1.5) # print e else: break # returns driver: return chrome
# -*- coding: utf-8 -*- from webdriverwrapper import Chrome driver = Chrome() driver.get('http://www.google.com') driver.click('gbqfsb') driver.wait_for_element(id_='top-content') print('Is text "Doodle" in whole page:', driver.contains_text('Doodles')) print('Is text "Doodle" in footer:', driver.get_elm('footer').contains_text('Doodles')) driver.quit()