我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用selenium.webdriver.support.ui.WebDriverWait()。
def _wait_for(self, wait_function, **kwargs): ''' Wrapper to handle the boilerplate involved with a custom wait. Parameters ---------- wait_function: func This can be a builtin selenium wait_for class, a special wait_for class that implements the __call__ method, or a lambda function timeout: int The number of seconds to wait for the given condition before throwing an error. Overrides WebRunner.timeout ''' try: wait = WebDriverWait(self.browser, kwargs.get('timeout') or self.timeout) wait.until(wait_function) except TimeoutException: if self.driver == 'Gecko': print("Geckodriver can't use the text_to_be_present_in_element_value wait for some reason.") else: raise
def verify_user_logged_in(selenium): # kdyz potrebujeme otestovat, ze na strance je nejaky konkretni element # el = selenium.find_element_by_xpath('//*[@id="messages"]/div/div') # vynechame nefunkcni identifikatory pro pripad, ze se zmeni HTML sablona el = selenium.find_element_by_id('messages') # kdyz potrebujeme zjistit, ze element obsahuje konkretni text assert 'Welcome back' in el.text # # ExplicitWait - kdyz se nahravaji veci pomoci JavaScriptu a nereloadne se cela stranka # WebDriverWait(selenium, 2).until( # EC.text_to_be_present_in_element((By.XPATH, '//*[@id="messages"]/div/div'), 'Welcome back') # ) # # kdyz potrebujeme otestovat, ze na strance neco neni # from selenium.common import exceptions # with pytest.raises(exceptions.NoSuchElementException, message='UNEXPECTED ELEMENT PRESENT'): # # musi hodit vyjimku, jinak failed # selenium.find_element_by_xpath('//*[@id="messages"]/div/div')
def _wait_until_search_input_field_appears(self, max_wait=5): """Waits until the search input field can be located for the current search engine Args: max_wait: How long to wait maximally before returning False. Returns: False if the search input field could not be located within the time or the handle to the search input field. """ def find_visible_search_input(driver): input_field = driver.find_element(*self._get_search_input_field()) return input_field try: search_input = WebDriverWait(self.webdriver, max_wait).until(find_visible_search_input) return search_input except TimeoutException as e: logger.error('{}: TimeoutException waiting for search input field: {}'.format(self.name, e)) return False
def _wait_until_search_param_fields_appears(self, max_wait=5): """Waits until the search input field contains the query. Args: max_wait: How long to wait maximally before returning False. """ def find_visible_search_param(driver): for _, field in self._get_search_param_fields().items(): input_field = driver.find_element(*field) if not input_field: return False return True try: fields = WebDriverWait(self.webdriver, max_wait).until(find_visible_search_param) return fields except TimeoutException as e: logger.error('{}: TimeoutException waiting for search param field: {}'.format(self.name, e)) return False
def _find_next_page_element(self): """Finds the element that locates the next page for any search engine. Returns: The element that needs to be clicked to get to the next page or a boolean value to indicate an error condition. """ if self.search_type == 'normal': selector = self.next_page_selectors[self.search_engine_name] try: # wait until the next page link is clickable WebDriverWait(self.webdriver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))) except (WebDriverException, TimeoutException): self._save_debug_screenshot() # raise Exception('{}: Cannot locate next page element: {}'.format(self.name, str(e))) try: return self.webdriver.find_element_by_css_selector(selector) except Exception: logger.error('failed find_element_by_css_selector, sleep 30 sec') time.sleep(30) pass elif self.search_type == 'image': self.page_down() if self.search_engine_name == 'google': return self.webdriver.find_element_by_css_selector('input._kvc') else: return True
def wait_for_element(self, driver, by, arg, visibiliy=True, timeout=10): try: if visibiliy: element = WebDriverWait(driver, timeout).until( EC.visibility_of_element_located((by, arg))) else: element = WebDriverWait(driver, timeout).until( EC.invisibility_of_element_located((by, arg))) except selenium.common.exceptions.TimeoutException: print("wait_for_element timeout: " + arg) return None return element
def assertTextPresent(self, by, value, expectedText, message = None, wait_for = None, \ ignoreCase = True): if not wait_for: wait_for = self.getDefaultWaitFor() self.assertElementPresent(by, value, message, wait_for) # lookup again to avoid stale element exception #self.assertElementPresent(by, value, message, wait_for) if not type(expectedText) is str: expectedText = expectedText.encode('unicode-escape') try: self.log("{3}Waiting a maximum of {0}s for text {1} in {2}{3}".format(\ wait_for, expectedText, self.describeElement(by, value),\ ". " + message if message else "")) return WebDriverWait(self.driver, wait_for).until(ElementHasText(\ (by, value), expectedText, ignoreCase)) except Exception: self.onFail(by, value, message, "Expected text {0}, but timed-out after {1} seconds.".\ format(expectedText, wait_for, traceback.format_exc()))
def submit_user_data(self): # Submit to Authent-Number Page (press button). wait = WebDriverWait(self.driver, timeout=6) try: self.driver.execute_script( 'document.getElementsByTagName("button")[0].click()' ) wait.until( EC.presence_of_element_located( (By.ID, 'idRandomPic') ) ) except: self.label_show_result.setText( '?????????????\n' + '???????????????????' )
def open_chat_with_user(self, username): assert username, "User name must be provided" icon_chat = WebDriverWait(self.driver, self.get_request_timeout_in_sec()).until( EC.presence_of_element_located(self.CHAT_ICON)) icon_chat.click() self.random_sleep_between_requests() input_search_field = WebDriverWait(self.driver, self.get_request_timeout_in_sec()).until( EC.presence_of_element_located(self.INPUT_SEARCH_FIELD)) input_search_field.clear() self.random_sleep_send_keys(input_search_field, username) self.random_sleep_between_requests() user_in_list = self.find_user_in_search_results(username) if user_in_list is not None: # select user from the search list user_in_list.click() self.random_sleep_between_requests() return True return False
def click_page(): driver = webdriver.Firefox() driver.get('http://yidao620c.github.io/archives/') driver.maximize_window() len1 = len(driver.find_elements_by_xpath( '//div[@class="post-archive"]/ul[@class="listing"]/li/a')) _log.info('????????...') for k in range(1, 100): logging.info('?{}???...'.format(k)) for i in range(0, len1): l_xpath = '(//div[@class="post-archive"]/ul[@class="listing"]/li/a)[{}]'.format(i + 1) ele = WebDriverWait(driver, 2).until( EC.presence_of_element_located((By.XPATH, l_xpath)) ) ele.click() driver.back() _log.info('all finished.') driver.close()
def _click_page(total_posts, pool_size, group_index): _log.info('?{}?: starting...'.format(group_index + 1)) if group_index > 0 and total_posts < pool_size * group_index: return # ???????? _driver = webdriver.PhantomJS() _driver.get('https://www.xncoding.com/archives/') global TRY_COUNT for k in range(1, TRY_COUNT + 1): # _log.info('?{}?: ?{}???...'.format(group_index + 1, k)) for i in range(pool_size * group_index, min(pool_size * (group_index + 1), total_posts)): l_xpath = '(//article/header/h1[@class="post-title"]/a[@class="post-title-link"])[{}]'.format(i + 1) ele = WebDriverWait(_driver, 2).until( EC.presence_of_element_located((By.XPATH, l_xpath)) ) ele.click() WebDriverWait(_driver, 5).until( EC.presence_of_element_located((By.XPATH, '//div[@class="post-body"]')) ) _driver.back() _log.info('?{}?: finished.'.format(group_index + 1)) _driver.close()
def just_click(): # ???????? _driver = webdriver.PhantomJS() _driver.get('https://www.xncoding.com/archives/') # driver.maximize_window() posts_count = len(_driver.find_elements_by_xpath( '//article/header/h1[@class="post-title"]/a[@class="post-title-link"]')) for cc in range(1, posts_count + 1): l_xpath = '(//article/header/h1[@class="post-title"]/a[@class="post-title-link"])[{}]'.format(cc) ele = WebDriverWait(_driver, 10).until( EC.element_to_be_clickable((By.XPATH, l_xpath)) ) _log.info('???{}???'.format(cc)) ele.click() WebDriverWait(_driver, 10).until( EC.presence_of_element_located((By.XPATH, '//div[@class="post-body"]')) ) _driver.back()
def participate(self): """Finish reading and send text""" try: logger.info("Entering participate method") ready = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.ID, 'finish-reading'))) stimulus = self.driver.find_element_by_id('stimulus') story = stimulus.find_element_by_id('story') story_text = story.text logger.info("Stimulus text:") logger.info(story_text) ready.click() submit = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.ID, 'submit-response'))) textarea = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.ID, 'reproduction'))) textarea.clear() text = self.transform_text(story_text) logger.info("Transformed text:") logger.info(text) textarea.send_keys(text) submit.click() return True except TimeoutException: return False
def expand_tree(self, node_count): """expand_tree expands treeview (dynatree) by clicking expander arrows one by one""" self.wait_for_frameworks() WebDriverWait(self.selenium, 10).until(EC.element_to_be_clickable(self.expander_locator)) WebDriverWait(self.selenium, 10).until(lambda _: len(self.expanders) == node_count) # least-work way to fight ElementNotVisibleException: Message: Cannot click on element, and # http://stackoverflow.com/questions/37781539/selenium-stale-element-reference-element-is-not-attached-to-the-page-document/38683022 def loop(): self.wait_for_frameworks() for expander in self.expanders: # type: WebElement node = self.retry_on_exception(NoSuchElementException, lambda: expander.find_element(By.XPATH, './..')) if self.is_expanded(node): continue self.wait_for_frameworks() expander.click() self.wait_for_frameworks() self.retry_on_exception(StaleElementReferenceException, loop) self.assert_tree_expanded()
def state(self): """ Check the current status of the alarm system. """ # Click the refresh button to verify the state if it was made somewhere else try: # Recheck the current status self._driver.get(self._driver.current_url) current_status = WebDriverWait(self._driver, self.timeout).until(EC.presence_of_element_located((self.STATUS_IMG[0], self.STATUS_IMG[1]))).text _LOGGER.debug('Fetched current status from system: {}'.format(current_status)) return current_status except (exceptions.NoSuchElementException, exceptions.NoSuchWindowException, exceptions.TimeoutException, urllib.error.URLError) as e: _LOGGER.warning('Error while checking alarm status. Attempting login again.') self._login() current_status = WebDriverWait(self._driver, self.timeout).until(EC.presence_of_element_located((self.STATUS_IMG[0], self.STATUS_IMG[1]))).text return current_status
def get_total_page(browser, url): browser.get(url) try: time.sleep(4) total_room = browser.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/h2/span').text if not total_room: return None if int(total_room) <= 30: return 1 total = WebDriverWait(browser, 30).until( EC.presence_of_element_located((By.XPATH, "/html/body/div[4]/div[1]/div[7]/div[2]/div/a[last()]")) ) if not total.text.isdigit(): total_page = browser.find_element_by_xpath('/html/body/div[4]/div[1]/div[7]/div[2]/div/a[last()-1]').text else: total_page = total.text return total_page except TimeoutException as e: print('????????25??????') time.sleep(25) return get_total_page(url)
def setUp(self): """Pretest settings.""" self.ps = PastaSauce() self.desired_capabilities['name'] = self.id() if not LOCAL_RUN: self.student = Student( use_env_vars=True, pasta_user=self.ps, capabilities=self.desired_capabilities ) self.teacher = Teacher( use_env_vars=True, pasta_user=self.ps, existing_driver=self.student.driver, capabilities=self.desired_capabilities ) else: self.student = Student( use_env_vars=True, ) self.teacher = Teacher( existing_driver=self.student.driver, use_env_vars=True ) self.wait = WebDriverWait(self.student.driver, Assignment.WAIT_TIME)
def setUp(self): """Pretest settings.""" if not LOCAL_RUN: self.ps = PastaSauce() self.desired_capabilities['name'] = self.id() self.student = Student( use_env_vars=True, pasta_user=self.ps, capabilities=self.desired_capabilities ) self.teacher = Teacher( existing_driver=self.student.driver, use_env_vars=True, pasta_user=self.ps, capabilities=self.desired_capabilities ) else: self.teacher = Teacher( use_env_vars=True ) self.student = Student( use_env_vars=True, existing_driver=self.teacher.driver, ) self.wait = WebDriverWait(self.student.driver, Assignment.WAIT_TIME)
def setUp(self): """Pretest settings.""" self.ps = PastaSauce() self.desired_capabilities['name'] = self.id() if not LOCAL_RUN: self.student = Student( use_env_vars=True, pasta_user=self.ps, capabilities=self.desired_capabilities ) self.teacher = Teacher( existing_driver=self.student.driver, use_env_vars=True, pasta_user=self.ps, capabilities=self.desired_capabilities ) else: self.student = Student( use_env_vars=True ) self.teacher = Teacher( use_env_vars=True, existing_driver = self.student.driver ) self.wait = WebDriverWait(self.student.driver, Assignment.WAIT_TIME)
def setUp(self): """Pretest settings.""" self.ps = PastaSauce() self.desired_capabilities['name'] = self.id() if not LOCAL_RUN: self.student = Student( use_env_vars=True, pasta_user=self.ps, capabilities=self.desired_capabilities ) else: self.student = Student( use_env_vars=True ) self.student.login() self.student.select_course(title='College Physics with Courseware') self.wait = WebDriverWait(self.student.driver, Assignment.WAIT_TIME) self.wait.until( expect.visibility_of_element_located(( By.XPATH, '//button[contains(@class,"practice")]' )) ).click() self.wait = WebDriverWait(self.student.driver, Assignment.WAIT_TIME) self.student.login()
def setUp(self): """Pretest settings.""" self.ps = PastaSauce() self.desired_capabilities['name'] = self.id() if not LOCAL_RUN: self.student = Student( use_env_vars=True, pasta_user=self.ps, capabilities=self.desired_capabilities ) else: self.student = Student( use_env_vars=True ) self.wait = WebDriverWait(self.student.driver, Assignment.WAIT_TIME) self.student.login()
def setUp(self): """Pretest settings.""" # login as admin, go to user menu, click admin option self.ps = PastaSauce() self.desired_capabilities['name'] = self.id() if not LOCAL_RUN: self.admin = Admin( use_env_vars=True, pasta_user=self.ps, capabilities=self.desired_capabilities ) else: self.admin = Admin( use_env_vars=True, ) self.wait = WebDriverWait(self.admin.driver, Assignment.WAIT_TIME) self.admin.login() self.admin.goto_admin_control()
def execises_logout(self): """Exercises logout helper.""" wait = WebDriverWait(self.driver, 3) try: wait.until( expect.element_to_be_clickable( (By.ID, 'navbar-dropdown') ) ).click() wait.until( expect.element_to_be_clickable( (By.CSS_SELECTOR, '[type="submit"]') ) ).click() self.page.wait_for_page_load() except NoSuchElementException: # Different page, but uses the same logic and link text self.find(By.CSS_SELECTOR, '[data-method]').click()
def url_links(self): """Generic webpage link finder format.""" # https://github.com/detro/ghostdriver/issues/169 @self.phantomjs_short_timeout def phantomjs_find_elements_by_tag_name(): return WebDriverWait(self.driver,3).until(lambda x: x.find_elements_by_tag_name('a')) elements = phantomjs_find_elements_by_tag_name() # get links in random order until max. per page k = 0 links = [] try: for a in sorted(elements,key=lambda k: random.random()): @self.phantomjs_short_timeout def phantomjs_get_attribute(): return a.get_attribute('href') href = phantomjs_get_attribute() if href is not None: links.append(href) k += 1 if k > self.max_links_per_page or self.link_count() == self.max_links_cached: break except Exception as e: if self.debug: print('.get_attribute() exception:\n{}'.format(e)) return links
def ensureLoadPageSuccessfully(browserWrapper, url, expectedConditions): try: browserWrapper.value.get(url) print u"browser.get end-----" except Exception, e: print "ensureLoadPageSuccessfully exception occurs : ", e if checkNeedRestartBrowserException(e): restartBrowser(browserWrapper) return False try: element = WebDriverWait(browserWrapper.value, 10).until(expectedConditions) except Exception, e: print "Error in wait page element load finish: ", e return False return True
def finished(self): try: # make sure js has executed wait = WebDriverWait(self, 10) wait.until(jQuery_load()) wait.until(jScript_load()) # deal with buttons which may change the content if self._experiment: _url = self.current_url self.click_buttons() if _url != self.current_url: logger.error("[error]page direct from %s to %s" % (_url, self.current_url)) page = Page(self.current_url, self.page_source, self._depth) self.onfinish(page) except Exception as e: # TimeoutException: logger.error("error!!") logger.error(e) traceback.print_exc() self.onfinish(None) # support with notation 'with'
def placeOrder(self): print(self.browser.title) print("Placing order.") wait = WebDriverWait(self.browser, 10) addToCart = self.browser.find_element_by_css_selector("input#add-to-cart-button") addToCart.click() time.sleep(10) print(self.browser.title) wait.until(EC.title_contains('Amazon.com Shopping Cart')) checkout = self.browser.find_element_by_css_selector("a#hlb-ptc-btn-native") checkout.click() time.sleep(10) print(self.browser.title) wait.until(EC.title_contains('Amazon.com Checkout')) placeOrder = self.browser.find_element_by_name("placeYourOrder1") placeOrder.click() time.sleep(20) print(self.browser.title) wait.until(EC.title_contains('Amazon.com Thanks You'))
def enter_search_term(browser, search_term): wait = WebDriverWait(browser, 10) try: search_bar = wait.until(EC.presence_of_element_located( (By.XPATH, "//input[@id='autocomplete-input']"))) button = wait.until(EC.element_to_be_clickable( (By.XPATH, "//button[@class='button-primary-alternative']"))) search_bar.click() time.sleep(randint(10, 15)) search_bar.clear() time.sleep(randint(10, 15)) search_bar.send_keys(search_term) time.sleep(randint(10, 15)) button.click() print("search-button has been clicked") time.sleep(randint(15, 20)) return True except (TimeoutException, NoSuchElementException) as e: print(str(e)) return False # Scrape the resulting page and move on to the next page until hitting the predefined lastpage. All results are stored in a csv-file
def find_element(driver, elem_path, by=CSS, timeout=TIMEOUT, poll_frequency=0.5): """ Find and return an element once located find_element locates an element on the page, waiting for up to timeout seconds. The element, when located, is returned. If not located, a TimeoutException is raised. Args: driver (selenium webdriver or element): A driver or element elem_path (str): String used to located the element by (selenium By): Selenium By reference timeout (int): Selenium Wait timeout, in seconds poll_frequency (float): Selenium Wait polling frequency, in seconds Returns: element: Selenium element Raises: TimeoutException: Raised when target element isn't located """ wait = WebDriverWait(driver, timeout, poll_frequency) return wait.until(EC.presence_of_element_located((by, elem_path)))
def find_elements(driver, elem_path, by=CSS, timeout=TIMEOUT, poll_frequency=0.5): """ Find and return all elements once located find_elements locates all elements on the page, waiting for up to timeout seconds. The elements, when located, are returned. If not located, a TimeoutException is raised. Args: driver (selenium webdriver or element): A driver or element elem_path (str): String used to located the element by (selenium By): Selenium By reference timeout (int): Selenium Wait timeout, in seconds poll_frequency (float): Selenium Wait polling frequency, in seconds Returns: list of elements: Selenium element Raises: TimeoutException: Raised when target element isn't located """ wait = WebDriverWait(driver, timeout, poll_frequency) return wait.until(EC.presence_of_all_elements_located((by, elem_path)))
def do_check_modules(self): logger.info('Checking ClipBucket modules') self.get('/admin_area/cb_mod_check.php') for module_element in self._driver.find_elements_by_class_name('well'): ui.WebDriverWait( self._driver, TIMEOUT).until( expected_conditions.visibility_of(module_element)) try: alert_element = module_element.find_element_by_class_name( 'alert') if alert_element: raise ClipBucketModuleError(alert_element.text) except exceptions.NoSuchElementException: # Lack of alert is good: the module is installed correctly. continue logger.info('Module check complete')
def wait_for(self, condition, *args, **kwargs): """Waits until `condition` is satisfied. If `condition` is not satisfied after a timeout period of 2 seconds, an exception is raised. :param condition: A function, method or other callable which will be called periodically to check\ whether a certain condition holds. It should return True if the condition holds,\ False otherwise. Extra positional and keyword arguments to this method are passed on as-is in the calls to `condition`. """ def wrapped(driver): try: return condition(*args, **kwargs) except Exception as ex: if isinstance(ex.args[0], CannotSendRequest): return False raise return WebDriverWait(self.web_driver, 2).until(wrapped)
def _click_action(self, locator, timeout=10.0): driver = self.driver try: element = WebDriverWait(driver, float(timeout)).until(EC.element_to_be_clickable(locator), self._get_trace()) except StaleElementReferenceException: driver.implicitly_wait(2) element = WebDriverWait(driver, float(timeout)).until(EC.element_to_be_clickable(locator), self._get_trace()) except WebDriverException: driver.implicitly_wait(1) element = WebDriverWait(driver, float(timeout)).until(EC.element_to_be_clickable(locator), self._get_trace()) try: element.click() except WebDriverException: sleep(1) element = WebDriverWait(driver, float(timeout)).until(EC.element_to_be_clickable(locator), self._get_trace()) element.click()
def get_search_page(search_text): url = "http://www.gsxt.gov.cn/index.html" # driver = webdriver.Chrome("/home/hee/driver/chromedriver") driver = webdriver.Chrome("E:\\virtualenv\\chromedriver.exe") driver.get(url) wait = WebDriverWait(driver, 20) element = wait.until(EC.presence_of_element_located((By.ID, "keyword"))) element.clear() element.send_keys(search_text) # element.send_keys(Keys.ENTER) time.sleep(random.uniform(1.0,2.0)) element = driver.find_element_by_id("btn_query") element.click() wait = WebDriverWait(driver, 30) element = wait.until( EC.presence_of_element_located((By.CLASS_NAME, "gt_box"))) time.sleep(random.uniform(2.0, 3.0)) return driver
def count(self): """ """ time.sleep(SLEEP) self.click() time.sleep(SLEEP) # wait for server response option_path = self.path + '/span[@data-value]' try: WebDriverWait(self.driver, TIMEOUT).until( EC.presence_of_element_located((By.XPATH, option_path)) ) options = self.driver.find_elements_by_xpath(option_path) return len(options) except (TimeoutException, socket.timeout): return 0
def parse_content(self,url): try: self.driver.get(url) except Exception,e: print "give up one detail" return "" try: element = WebDriverWait(self.driver, 30).until( EC.presence_of_all_elements_located((By.TAG_NAME, 'table')) ) print 'element:\n', element except Exception, e: print Exception, ":", e print "wait failed" page_source = self.driver.page_source bs_obj = BeautifulSoup(page_source, "lxml") return '%s'%bs_obj.find('td', class_='a-content').p.get_text().encode('utf-8','ignore')
def parse_content(self,url): try: self.driver.get(url) except Exception,e: print "give up one detail" return "" try: element = WebDriverWait(self.driver, 30).until( EC.presence_of_all_elements_located((By.ID, 'a-row dealTile')) ) print 'element:\n', element except Exception, e: print Exception, ":", e print "wait failed" page_source = self.driver.page_source bs_obj = BeautifulSoup(page_source, "lxml") return '%s'%bs_obj.find('td', class_='a-content').p.get_text().encode('utf-8','ignore')
def link_is_present(driver, delay, selector, index, results_page): """ verify that the link selector is present and print the search details to console. This method is particularly useful for catching the last link on the last page of search results """ try: WebDriverWait(driver, delay).until( EC.presence_of_element_located( (By.XPATH, selector) ) ) print("**************************************************") print("\nScraping data for result {}" \ " on results page {} \n".format(index, results_page)) except Exception as e: print(e) if index < 25: print("\nWas not able to wait for job_selector to load. Search " \ "results may have been exhausted.") return True else: return False else: return True
def login(self): """login to linkedin then wait 3 seconds for page to load""" # Enter login credentials WebDriverWait(self.driver, 120).until( EC.element_to_be_clickable( (By.ID, "session_key-login") ) ) elem = self.driver.find_element_by_id("session_key-login") elem.send_keys(self.username) elem = self.driver.find_element_by_id("session_password-login") elem.send_keys(self.password) # Enter credentials with Keys.RETURN elem.send_keys(Keys.RETURN) # Wait a few seconds for the page to load time.sleep(3)
def authenticate(self, username, password): """Log in to Instagram with the provided credentials.""" print('\nLogging in…') self.driver.get('https://www.instagram.com') # Go to log in login_link = WebDriverWait(self.driver, 5).until( EC.presence_of_element_located((By.LINK_TEXT, 'Log in')) ) login_link.click() # Authenticate username_input = self.driver.find_element_by_xpath( '//input[@placeholder="Username"]' ) password_input = self.driver.find_element_by_xpath( '//input[@placeholder="Password"]' ) username_input.send_keys(username) password_input.send_keys(password) password_input.send_keys(Keys.RETURN) time.sleep(1)
def getDriverWithProxySupport(self, proxy_host, proxy_port): if self.debug == False: self.display = Display(visible=0, size=(1920, 1080)) self.display.start() profile = self.getWebDriverProfile() profile.set_preference("network.proxy.type", 1) profile.set_preference("network.proxy.http", proxy_host) profile.set_preference("network.proxy.http_port", proxy_port) profile.set_preference("network.proxy.https", proxy_host) profile.set_preference("network.proxy.https_port", proxy_port) profile.set_preference("network.proxy.ssl", proxy_host) profile.set_preference("network.proxy.ssl_port", proxy_port) profile.update_preferences() capabilities = webdriver.DesiredCapabilities().FIREFOX capabilities["marionette"] = False newdriver = webdriver.Firefox(firefox_profile=profile, capabilities=capabilities) #newdriver = webdriver.Firefox(firefox_profile=profile) self.wait = ui.WebDriverWait(newdriver, 10) # timeout after 10 seconds return newdriver
def test_simple_ip_search_should_return_result(selenium, base_url): """Tests a search for an IP address""" selenium.get('{}/'.format(base_url)) query = selenium.find_element_by_id('query') search_button = selenium.find_element_by_css_selector( "input.button[type='submit']") ipaddr = "192.168.42.42" query.send_keys(ipaddr) search_button.click() caption = WebDriverWait(selenium, 15).until( EC.text_to_be_present_in_element((By.TAG_NAME, "caption"), ipaddr) ) caption = selenium.find_element_by_tag_name('caption') assert ipaddr in caption.text
def find_id_with_timeout(driver, ID, timeout=TIMEOUT): return WebDriverWait(driver, timeout).until( EC.presence_of_element_located((By.ID, ID)) )
def find_class_with_timeout(driver, CLASS, timeout=TIMEOUT): return WebDriverWait(driver, timeout).until( EC.presence_of_element_located((By.CLASS_NAME, CLASS)) )
def find_xpath_with_timeout(driver, XPATH, timeout=TIMEOUT): return WebDriverWait(driver, timeout).until( EC.presence_of_element_located((By.XPATH, XPATH)) )
def find_visible_id_with_timeout(driver, ID, timeout=TIMEOUT): return WebDriverWait(driver, timeout).until( EC.visibility_of_element_located((By.ID, ID)) )
def _goto_next_page(self): """ Click the next page element, Returns: The url of the next page or False if there is no such url (end of available pages for instance). """ next_url = '' element = self._find_next_page_element() if hasattr(element, 'click'): next_url = element.get_attribute('href') try: element.click() except WebDriverException: # See http://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error # first move mouse to the next element, some times the element is not visibility selector = self.next_page_selectors[self.search_engine_name] if selector: try: next_element = WebDriverWait(self.webdriver, 5).until( EC.presence_of_element_located((By.CSS_SELECTOR, selector))) webdriver.ActionChains(self.webdriver).move_to_element(next_element).perform() # wait until the next page link emerges WebDriverWait(self.webdriver, 8).until( EC.visibility_of_element_located((By.CSS_SELECTOR, selector))) element = self.webdriver.find_element_by_css_selector(selector) next_url = element.get_attribute('href') element.click() except WebDriverException: pass # wait until the next page was loaded if not next_url: return False else: return next_url
def wait_until_title_contains_keyword(self): try: WebDriverWait(self.webdriver, 5).until(EC.title_contains(self.query)) except TimeoutException: logger.debug(SeleniumSearchError( '{}: Keyword "{}" not found in title: {}'.format(self.name, self.query, self.webdriver.title)))
def wait_until_serp_loaded(self): def wait_until_keyword_in_url(driver): try: return quote(self.query) in driver.current_url or \ self.query.replace(' ', '+') in driver.current_url except WebDriverException: pass WebDriverWait(self.webdriver, 5).until(wait_until_keyword_in_url)