def scrollIntoCenter(self): scrollElementIntoMiddle = "var viewPortHeight = Math.max(document.documentElement.clientHeight, " \ "window.innerHeight || 0); var elementTop = arguments[0].getBoundingClientRect().top;"\ "window.scrollBy(0, elementTop-(viewPortHeight/2));" Browser.get_driver().execute_script(scrollElementIntoMiddle, self.get_element())
def _initialize_browsers(self, browser_params): """ initialize the browser classes, each its unique set of parameters """ browsers = list() for i in xrange(self.num_browsers): browsers.append(Browser(self.manager_params, browser_params[i])) return browsers
def exists(self): try: WebDriverWait(Browser.get_driver(), 1).until(EC.presence_of_element_located((self.by, self.locator))) return True except: return False
def press_key(self, key, use_action_chains=False): if not use_action_chains: self.get_element().send_keys(key) else: chains = ActionChains(driver=Browser.get_driver()) chains.send_keys(key).perform() return self
def is_clickable(by, locator): try: WebDriverWait(Browser.get_driver(), 1).until( EC.element_to_be_clickable((by, locator))) return True except: return False
def _initialize_browsers(self, browser_params): """ initialize the browser classes, each with a unique set of parameters """ browsers = list() for i in xrange(self.num_browsers): # update crawl table # TODO: update DB with browser.browser_settings for each browser manager initialized cur = self.db.cursor() query_successful = False crawl_id = -1 while not query_successful: try: cur.execute( "INSERT INTO crawl (task_id, profile, browser, headless, proxy, debugging, " "disable_flash) VALUES (?,?,?,?,?,?,?)", (self.task_id, browser_params[i]['profile_tar'], browser_params[i]['browser'], browser_params[i]['headless'], browser_params[i]['proxy'], browser_params[i]['debugging'], browser_params[i]['disable_flash'])) self.db.commit() crawl_id = cur.lastrowid query_successful = True except OperationalError: time.sleep(0.1) pass browser_params[i]['crawl_id'] = crawl_id browser_params[i]['aggregator_address'] = self.aggregator_address browser_params[i]['logger_address'] = self.logger_address browsers.append(Browser(browser_params[i])) return browsers
def _initialize_browsers(self, browser_params): """ initialize the browser classes, each its unique set of params """ browsers = list() for i in range(self.num_browsers): browser_params[i][ 'crawl_id'] = self.data_aggregator.get_next_crawl_id() browsers.append(Browser(self.manager_params, browser_params[i])) return browsers
def click(self, wait=10, use_action_chains=False): self.wait_to_be_clickable(wait) initial_handles = Browser.get_driver().window_handles if use_action_chains: ui_object = self.get_element() ActionChains(Browser.get_driver()).move_to_element(ui_object).click().perform() else: try: self.get_element().click() except Exception as error: if "Other element would receive the click" in error: self.scrollIntoCenter() self.get_element().click() else: raise error if len(Browser.get_driver().window_handles) > len(initial_handles): Browser.switch_to_latest_active_window() return self
def wait_to_disappear(self, seconds=10, ignore_error=False): start = time.time() while (time.time() - start) < seconds: try: WebDriverWait(Browser.get_driver(), seconds).until(EC.invisibility_of_element_located((self.by, self.locator))) except Exception as error: if not ignore_error: raise AssertionError("Locator: {} did not disappear in {} seconds! Error: {}" .format(self.locator, seconds, error.message)) return self
def navigate(button): Browser.create_new_driver(Browser.CHROME) Browser.get_driver().get("https://www.hackerrank.com/dashboard") button_object = UiObject(By.XPATH, "//a[contains(@class, 'nav-link')]/span[text() = '{}']/parent::a".format(button)) title = UiObject(By.XPATH, "//h1") button_object.click() time.sleep(2) print ("Thread: {} URL: {} Title: {} Button Class: {}".format(threading.currentThread(), Browser.get_driver().current_url, title.get_text(), button_object.get_attribute("class"))) Browser.shutdown()
def initialize_browsers(self, browser_params): """ initialize the browsers, each with a unique set of parameters """ browsers = list() for i in xrange(self.num_browsers): # update crawl table # TO DO: update DB with browser.browser_settings for each browser manager initialized cur = self.db.cursor() query_successful = False crawl_id = -1 while not query_successful: try: cur.execute( "INSERT INTO crawl (task_id, profile, browser, headless, proxy, debugging, " "timeout, disable_flash) VALUES (?,?,?,?,?,?,?,?)", (self.task_id, browser_params[i]['profile_tar'], browser_params[i]['browser'], browser_params[i]['headless'], browser_params[i]['proxy'], browser_params[i]['debugging'], browser_params[i]['timeout'], browser_params[i]['disable_flash'])) self.db.commit() crawl_id = cur.lastrowid query_successful = True except OperationalError: time.sleep(2) pass browser_params[i]['crawl_id'] = crawl_id browser_params[i]['aggregator_address'] = self.aggregator_address browsers.append(Browser(browser_params[i])) # Update our DB with the random browser settings # These are found within the scope of each instance of Browser in the browsers list for item in browsers: if not item.browser_settings['extensions']: extensions = 'None' else: extensions = ','.join(item.browser_settings['extensions']) screen_res = str(item.browser_settings['screen_res']) ua_string = str(item.browser_settings['ua_string']) self.sock.send(( "UPDATE crawl SET extensions = ?, screen_res = ?, ua_string = ? \ WHERE crawl_id = ?", (extensions, screen_res, ua_string, item.crawl_id))) return browsers
def is_checked(self): return Browser.get_driver().execute_script("return arguments[0].checked;", self.get_element())
def get_all_elements(self, wait=10): self.wait_to_appear(wait) return Browser.get_driver().find_elements(self.by, self.locator)
def mouse_over(self): ui_object = self.get_element() ActionChains(Browser.get_driver()).move_to_element(ui_object).perform() return self
def scrollIntoView(self): Browser.get_driver().execute_script("arguments[0].scrollIntoView()", self.get_element()) return self