def profile(self, value): if not isinstance(value, FirefoxProfile): raise WebDriverException( "When passing in a value to profile," " please pass in a FirefoxProfile object.") self._profile = value
from selenium.common.exceptions import WebDriverException from coyote_framework.log import Logger class WebDriverWrapperException(WebDriverException): def __init__(self, driver_wrapper, msg='WebDriverWrapper Exception', execute_on_error=None): js_executor = JavascriptExecutor.JavascriptExecutor(driver_wrapper) error_message = None try: # error_message sometimes has encoding problems error_message = "Message: {} || Page Title: {} || Current URL: {}"\ .format(msg, driver_wrapper.driver.title, driver_wrapper.driver.current_url) # insert the error message into the page js_executor.execute_template('messageInjectorTemplate', {'message': error_message}) except Exception, e: error_message = 'Unable to build error message: {}'.format(e) if error_message is None else error_message finally: Logger.get().warn(error_message) WebDriverException.__init__(self, error_message) # pass it an anonymous function to execute on instantiation if execute_on_error is not None and hasattr(execute_on_error, '__call__'): execute_on_error()
def find_element(self, locate, index=0, max_times=20, delay=0.5, displayed=True): """ 定位元素方法,用于定位多个属性相同的元素,当index为None时返回的是符合匹配规则的元素列表 :param locate: 元素定位 :param index: 序号,第一个为0,第二个为1,以此类推,默认为0,当为None时返回所有符合条件的元素 :param max_times: 最大循环次数,默认为20次 :param delay: 延时,默认为0.5秒 :param displayed: 是否必须等待到元素可见才算找到元素,默认为是 :return: """ # 是否报错的标志 error = False # 是否已定位到的标志 flag = False for i in range(max_times): # 当前已定位到num个元素 num = 0 try: elements = self.driver.find_elements(*locate) if elements == None or elements == []: if i < max_times-1: logger.info("The server doesn't send a response and the position will be retried after {} seconds!".format(delay)) continue if index == None: for element in elements: if element.is_displayed() == False and displayed == True: if i < max_times-1: logger.info("The elements has been positioned but one of the elements is not visible and will be repositioned after {} seconds!".format(delay)) break else: num += 1 if num == len(elements): flag = True logger.info("Positioned the elements {}.".format(locate)) return elements else: element = elements[index] if element.is_displayed() == False and displayed == True: logger.info("The element has been positioned but the element is not visible and will be repositioned after {} seconds!".format(delay)) else: flag = True logger.info("Positioned the element {}[{}].".format(locate, index)) return element except WebDriverException as e: if i == max_times - 1: error = True logger.error(e) raise WebDriverException(e.msg, self.daf.get_screenshot(self.driver)) if index == None: logger.info("The elements {} are not successfully positioned and will be retried after hibernation.".format(locate)) else: logger.info("The element {}[{}] is not successfully positioned and will be retried after hibernation.".format(locate, index)) finally: if i < max_times-1 and flag != True: self.sleep(delay) elif i == max_times-1 and error != True: if index == None: logger.error("Failed to position the elements {}!".format(locate)) raise WebDriverException("Failed to position the elements {}!".format(locate)) else: logger.error("Failed to position the element {}[{}]!".format(locate, index)) raise WebDriverException("Failed to position the element {}[{}]!".format(locate, index))
def near(self, element_or_locator_distance=None): if element_or_locator_distance is None: raise WebDriverException("Element or locator or distance must be given when calling above method") self.filters.append({"kind": "near", "args": [element_or_locator_distance]}) return self
def dummy(): raise WebDriverException("hoge:fuga:huhuhu")
def assume_untrusted_cert_issuer(self, value): if value not in [True, False]: raise WebDriverException("Please pass in a Boolean to this call") self.set_preference("webdriver_assume_untrusted_issuer", value)
def with_tag_name(tag_name): if tag_name is None: raise WebDriverException("tag_name can not be null") return RelativeBy({"css selector": tag_name})
def wait_for_true(fn): start_time = time.time() while not wait_for(fn): if time.time() - start_time > TIMEOUT: raise WebDriverException('Function never evaluated to True')
def download_url(url, uidx): # Setup webdriver profile = webdriver.FirefoxProfile() profile.set_preference('browser.download.folderList', 2) # Don't use default dir profile.set_preference('browser.download.manager.showWhenStarting', False) profile.set_preference('browser.download.dir', download_path) profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/x-zip-compressed') driver = webdriver.Firefox(firefox_profile=profile, executable_path=gecko_path) wait = WebDriverWait(driver, DRIVER_TIMEOUT) # Login driver.maximize_window() driver.get('https://libproxy.wlu.ca/login?url=http://www.nexisuni.com') try: e = wait.until(EC.presence_of_element_located((By.ID, 'username'))) except WebDriverException as e: print('unable to login, quitting') driver.quit() quit() e.send_keys(credentials['username']) e = driver.find_element_by_id('password') e.send_keys(credentials['password']) e.send_keys(Keys.ENTER) time.sleep(5) # waits for login driver.get(url) for attempt in range(MAXIMUM_RETRIES): try: # group similar if attempt == 0: e = wait.until( EC.element_to_be_clickable( (By.XPATH, "//span[@data-action='toggleduplicates']"))) e.click() # page count wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) e = wait.until( EC.element_to_be_clickable( (By.XPATH, "//nav[@class='pagination']/ol/li[6]/a"))) total_pages = int(e.text) n_selected = 0 page = 1 batch = 1 while page <= total_pages: # select everything wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) driver.find_element_by_xpath( "//input[@data-action='selectall']").click() # count selected and verify n_selected += len( driver.find_elements_by_css_selector( "input:checked[data-docid^='urn:']")) wait.until( EC.text_to_be_present_in_element( (By.XPATH, "//button[@data-action='viewtray']/span[1]"), str(n_selected))) # download if page % 10 == 0 or page == total_pages: filename = f'idx_{uidx}_batch_{batch:d}' if not Path(str(download_path) + filename + '.ZIP' ).is_file(): # if file D.N.E try downloading # click download button wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) wait.until( EC.element_to_be_clickable(( By.XPATH, "//button[@class='has_tooltip' and @data-action='downloadopt']" ))).click() # basic options wait.until(EC.element_to_be_clickable( (By.ID, "Rtf"))).click() driver.find_element_by_id("SeparateFiles").click() # filename driver.find_element_by_id("FileName").clear() driver.find_element_by_id("FileName").send_keys( filename) # formatting options driver.find_element_by_id( 'tab-FormattingOptions').click() # cover page chk = wait.until( EC.element_to_be_clickable( (By.ID, "IncludeCoverPage"))).get_attribute('checked') if chk == 'true': driver.find_element_by_id( "IncludeCoverPage").click() # first & lastname in footer if driver.find_element_by_id( "DisplayFirstLastNameEnabled").get_attribute( 'checked') == 'true': driver.find_element_by_id( "DisplayFirstLastNameEnabled").click() # page numbering if driver.find_element_by_id( "PageNumberSelected").get_attribute( 'checked') == 'true': driver.find_element_by_id( "PageNumberSelected").click() # embedded reference links if driver.find_element_by_id( "EmbeddedReferences").get_attribute( 'checked') == 'true': driver.find_element_by_id( "EmbeddedReferences").click() # bold search terms if driver.find_element_by_id( 'SearchTermsInBoldTypeEnabled').get_attribute( 'checked') == 'true': driver.find_element_by_id( 'SearchTermsInBoldTypeEnabled').click() # italicize terms if driver.find_element_by_id( 'SearchTermsInItalicTypeEnabled' ).get_attribute('checked') == 'true': driver.find_element_by_id( 'SearchTermsInItalicTypeEnabled').click() # underline search terms if driver.find_element_by_id( 'SearchTermsUnderlinedEnabled').get_attribute( 'checked') == 'true': driver.find_element_by_id( 'SearchTermsUnderlinedEnabled').click() # bold reporter page numbers if driver.find_element_by_id( 'DisplayPaginationInBoldEnabled' ).get_attribute('checked') == 'true': driver.find_element_by_id( 'DisplayPaginationInBoldEnabled').click() # download driver.execute_script( 'arguments[0].click();', driver.find_element_by_xpath( "//button[@data-action='download']")) start_time = time.time() while True: if os.path.exists( str(download_path) + filename + '.ZIP'): if not os.path.exists( str(download_path) + filename + '.ZIP.part'): break time.sleep(2) if time.time() - start_time > DOWNLOAD_TIMEOUT: raise WebDriverException('download timeout') n_selected = 0 batch += 1 else: # File exists, so deselect all and reset selection count wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) wait.until( EC.element_to_be_clickable(( By.XPATH, "//button[@data-action='viewtray']"))).click() wait.until( EC.element_to_be_clickable( (By.XPATH, "//button[@data-action='cancel']"))).click() wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) wait.until( EC.element_to_be_clickable( (By.XPATH, "//button[@data-action='confirm']"))).click() n_selected = 0 batch += 1 if page == total_pages: driver.quit() return True # navigate to next page wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) e = driver.find_element_by_xpath( "//nav[@class='pagination']/ol/li[@class='current']/span") old_page = int(e.text) driver.execute_script( 'arguments[0].click();', driver.find_element_by_xpath( "//nav[@class='pagination']/ol/li[last()]/a")) wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) e = driver.find_element_by_xpath( "//nav[@class='pagination']/ol/li[@class='current']/span") if int(e.text) == (old_page + 1): page += 1 else: raise WebDriverException('did not navigate to next page') except (WebDriverException, ValueError, TypeError) as ex: # make sure nothing is selected, this can occur if there is an exception and part of page is selected try: wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) sel_count = wait.until( EC.presence_of_element_located( (By.XPATH, "//button[@data-action='viewtray']/span[1]"))).text if sel_count != '' and sel_count is not None: wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) wait.until( EC.element_to_be_clickable( (By.XPATH, "//button[@data-action='viewtray']"))).click() wait.until( EC.element_to_be_clickable( (By.XPATH, "//button[@data-action='cancel']"))).click() wait.until( EC.invisibility_of_element_located( (By.XPATH, "//div[@class='box']"))) wait.until( EC.element_to_be_clickable( (By.XPATH, "//button[@data-action='confirm']"))).click() n_selected = 0 except WebDriverException as wex_inner: pass print(f'[{uidx}] <{str(ex).strip()}>') driver.get(url) time.sleep(2.5) else: # If there were no exceptions this block gets executed driver.quit() return True else: driver.quit() print(f'[{uidx}] max retry failed: aborting url') return False
def driver_init(request): import os from selenium import webdriver from selenium.common.exceptions import WebDriverException test_name = request.node.name # from https://github.com/saucelabs-sample-test-frameworks/Python-Pytest-Selenium/blob/master/conftest.py # can't get this to work for multiple browsers - need to run in parallel? # credit card test is slow due to one card digit at a time # idleTimeout must be greater than webdriver timeouts (basetest.py SECONDS_WAIT) or they dont throw exceptoins desired_caps = {} # "recordScreenshots": "false", # "extendedDebugging": "false", desired_caps["videoUploadOnPass"] = "******" desired_caps["name"] = request.node.name desired_caps["commandTimeout"] = 15 desired_caps["idleTimeout"] = 15 desired_caps["recordVideo"] = "true" if os.getenv("device") == "desktop": desired_caps["build"] = "OpenEdX-UI-QA-desktop" desired_caps["tags"] = [ "online-qa.ucsd.edu", "ui", "qa", "acceptance", "desktop", ] desired_caps["maxDuration"] = 150 # get soa test > 90 sec if os.getenv("browser") == "chrome": desired_caps["browserName"] = "chrome" desired_caps["version"] = "latest-1" desired_caps["platform"] = "Mac OS X 10.15" elif os.getenv("browser") == "safari": desired_caps["browserName"] = "safari" # downgraded from "latest" and 10.15 on 2020-08-14 due to test_soa function error # selenium.common.exceptions.InvalidArgumentException: Message: Request body does not contain required parameter 'handle'. # see https://github.com/SeleniumHQ/selenium/issues/6431 desired_caps["version"] = "latest-1" desired_caps["platform"] = "Mac OS X 10.13" elif os.getenv("browser") == "firefox": desired_caps["browserName"] = "firefox" desired_caps["version"] = "latest-1" # no jquery in ff on windows desired_caps["platform"] = "Mac OS X 10.15" # getting java would like to record popup desired_caps["recordScreenshots"] = "false" desired_caps["recordVideo"] = "false" elif os.getenv("browser") == "edge": desired_caps["browserName"] = "MicrosoftEdge" desired_caps["version"] = "latest-1" desired_caps["platform"] = "Windows 10" else: print("BAD") pytest.exit( "unsupported browser: " + os.getenv("browser") + "; supported desktop browsers: chrome | safari | edge | firefox" ) # http://appium.io/docs/en/writing-running-appium/web/mobile-web/ elif os.getenv("device") == "mobile": # chrome not supported on ios simulator desired_caps["build"] = "OpenEdX-UI-QA-mobile" desired_caps["tags"] = [ "online-qa.ucsd.edu", "ui", "qa", "acceptance", "mobile", ] # desired_caps['appiumVersion'] = "1.17.1" if os.getenv("browser") == "chrome": desired_caps["browserName"] = "Chrome" desired_caps["deviceName"] = "Android Emulator" desired_caps["platformVersion"] = "8.0" desired_caps["platformName"] = "Android" elif os.getenv("browser") == "safari": desired_caps["browserName"] = "Safari" desired_caps["deviceName"] = "iPhone XS Simulator" desired_caps["platformVersion"] = "13.2" desired_caps["platformName"] = "iOS" else: pytest.exit( "unsupported browser: " + os.getenv("browser") + "; supported mobile browsers: chrome | safari" ) # super slow desired_caps["maxDuration"] = 180 else: pytest.exit( "unsupported device: " + os.getenv("device") + "; supported devices: desktop | mobile" ) # The command_executor tells the test to run on Sauce, while the desired_capabilities # parameter tells us which browsers and OS to spin up. web_driver = webdriver.Remote( "http://" + os.getenv("SAUCE_USERNAME") + ":" + os.getenv("SAUCE_ACCESS_KEY") + "@ondemand.saucelabs.com:80/wd/hub", desired_capabilities=desired_caps, ) # https://www.blazemeter.com/blog/improve-your-selenium-webdriver-tests-with-pytest/ # for every test class object that is decorated with @pytest.mark.userfixtures("driver_init") # we set the attribute "driver". So in the test class we can access the web driver # instance with self.driver. You can notice that we haven't setup the WebDriver instance # implicitly, instead we are just using it via reference to the self.driver. # This is the implementation of the dependency injection: # in the test class or function we don't know how the WebDriver was initialized # or what is going to be happen later, we are just using the instance. session = request.node # for item in session.items: # cls = item.getparent(pytest.Class) # setattr(cls.obj,"driver",web_driver) request.cls.driver = web_driver # This is specifically for SauceLabs plugin. # In case test fails after selenium session creation having this here will help track it down. # creates one file per test non ideal but xdist is awful if web_driver is not None: print( "SauceOnDemandSessionID={} job-name={}".format( web_driver.session_id, test_name ) ) else: raise WebDriverException("Never created!") # yield # provide the fixture value to test methods yield web_driver # Teardown starts here # report results # use the test result to send the pass/fail status to Sauce Labs sauce_result = "failed" if request.node.rep_call.failed else "passed" print("sauce_result: " + sauce_result) web_driver.execute_script("sauce:job-result={}".format(sauce_result)) web_driver.quit() # teardown; this gets called after tests are complete
def driver(request, browser_config): platform = parsePlatform(request.node.name) browserName = parseBrowserName(request.node.name) sentry_sdk.set_tag("platform", platform) sentry_sdk.set_tag("browserName", browserName) sentry_sdk.set_context("request.node.name", {"request_node_name": request.node.name}) sentry_sdk.capture_message("Started Pytest for %s - %s" % (platform, browserName)) # if the assignment below does not make sense to you please read up on object assignments. # The point is to make a copy and not mess with the original test spec. desired_caps = dict() desired_caps.update(browser_config) # Represents a specific browser test_name = request.node.name build_tag = environ.get('BUILD_TAG', None) tunnel_id = environ.get('TUNNEL_IDENTIFIER', None) username = environ.get('SAUCE_USERNAME', None) access_key = environ.get('SAUCE_ACCESS_KEY', None) selenium_endpoint = "https://%s:%[email protected]:443/wd/hub" % ( username, access_key) desired_caps['build'] = build_tag # we can move this to the config load or not, also messing with this on a test to test basis is possible :) desired_caps['tunnelIdentifier'] = tunnel_id desired_caps['name'] = test_name executor = RemoteConnection(selenium_endpoint, resolve_ip=False) browser = webdriver.Remote(command_executor=executor, desired_capabilities=desired_caps, keep_alive=True) browser.implicitly_wait(10) # This is specifically for SauceLabs plugin. # In case test fails after selenium session creation having this here will help track it down. # creates one file per test non ideal but xdist is awful if browser is not None: print("SauceOnDemandSessionID={} job-name={}".format( browser.session_id, test_name)) else: sentry_sdk.capture_message("Never created - case test failed: %s %s" % (browser.session_id, test_name)) raise WebDriverException("Never created!") # This is the test running yield browser session_id = browser.session_id # Teardown starts here # report results # use the test result to send the pass/fail status to Sauce Labs sauce_result = "failed" if request.node.rep_call.failed else "passed" if sauce_result == "failed": sentry_sdk.set_context("sauce_result", { "browser_session_id": session_id, "test_name": test_name }) sentry_sdk.capture_message("Sauce Result: %s" % (sauce_result)) browser.execute_script("sauce:job-result={}".format(sauce_result)) browser.quit() # If the test errors on not finding a button, then this should still execute # because it's part of Teardown which always runs sentry_sdk.set_tag("session_id", session_id) sentry_sdk.capture_message("Session done")
def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False, file_detector=None, session_id=None): """ Create a new driver that will issue commands using the wire protocol. :Args: - command_executor - Either a string representing URL of the remote server or a custom remote_connection.RemoteConnection object. Defaults to 'http://127.0.0.1:4444/wd/hub'. - desired_capabilities - A dictionary of capabilities to request when starting the browser session. Required parameter. - browser_profile - A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object. Only used if Firefox is requested. Optional. - proxy - A selenium.webdriver.common.proxy.Proxy object. The browser session will be started with given proxy settings, if possible. Optional. - keep_alive - Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive. Defaults to False. - file_detector - Pass custom file detector object during instantiation. If None, then default LocalFileDetector() will be used. """ if desired_capabilities is None: raise WebDriverException("Desired Capabilities can't be None") if not isinstance(desired_capabilities, dict): raise WebDriverException( "Desired Capabilities must be a dictionary") if proxy is not None: warnings.warn("Please use FirefoxOptions to set proxy", DeprecationWarning) proxy.add_to_capabilities(desired_capabilities) if (desired_capabilities["browserName"] == 'firefox'): command_executor = FirefoxRemoteConnection( remote_server_addr=command_executor) self.command_executor = command_executor if type(self.command_executor) is bytes or isinstance( self.command_executor, str): self.command_executor = RemoteConnection(command_executor, keep_alive=keep_alive) self._is_remote = True self.session_id = session_id # added self.capabilities = dict(desired_capabilities) self.error_handler = ErrorHandler() self.start_client() if browser_profile is not None: warnings.warn("Please use FirefoxOptions to set browser profile", DeprecationWarning) if session_id: if desired_capabilities["browserName"] != "firefox": self.connect_to_session(desired_capabilities) # added else: pass else: self.start_session(desired_capabilities, browser_profile) self._switch_to = SwitchTo(self) self._mobile = Mobile(self) self.file_detector = file_detector or LocalFileDetector() self.w3c = True # added hardcoded
def get(driver: str, driver_timeout: int) -> str: if driver == webdriver: return driver else: raise WebDriverException(driver)
def wait_text_gone(self, locate, text, index=0, max_times=20, delay=0.5, displayed=True): """ 等待元素的文本内容变更 :param locate: 元素定位 :param text: 希望消失的文本内容,在本方法中该参数可以是字符串或列表,为字符串时表示等待目标元素的文本内容不包含text,为列表时表示等待目标元素与text的文本内容一一对应且不相等 :param index: 序号,第一个为0,第二个为1,以此类推,默认为None,为默认值时表示等待所有目标元素的文本内容变更 :param max_times: 最大循环次数,默认为20次 :param delay: 延时,默认为0.5秒 :param displayed: 是否必须等待到元素可见才算找到元素,默认为True :return: """ for i in range(max_times): num = 0 try: if index != None: elements_text = self.find_element(locate, index, max_times, delay, displayed).text else: elements_text = [element.text for element in self.find_element(locate, index, max_times, delay, displayed)] except WebDriverException as e: if i < max_times-1: continue else: raise e logger.info('The text of the target element is "{}",the actual text is "{}"...\nStart matching>>>>>>'.format(text,elements_text)) if type(text) == list: if type(elements_text) == list and len(text) != len(elements_text): logger.info('Matching success!') break if type(elements_text) == str and len(text) != 1: logger.error("The length of the entered text list({}) is inconsistent with the actual string(1).".format(len(text))) raise Exception("The length of the entered text list({}) is inconsistent with the actual string(1).".format(len(text))) if type(elements_text) == list: for j,element_text in enumerate(elements_text): if element_text != text[j]: num += 1 elif element_text == text[j] and i < max_times-1: self.sleep(delay) break else: logger.error('Matching Failed!\nTarget : {} \nActual : {}'.format(text, elements_text)) raise WebDriverException('Matching Failed!\nTarget : {} \nActual : {}'.format(text, elements_text)) if num == len(elements_text): logger.info('Matching success!') break elif type(elements_text) == str: if elements_text != text[0]: logger.info('Matching success!') break elif i < max_times-1: self.sleep(delay) else: logger.error('Matching Failed!\nTarget : {} \nActual : {}'.format(text, elements_text)) raise WebDriverException('Matching Failed!\nTarget : {} \nActual : {}'.format(text, elements_text)) elif type(text) == str: if type(elements_text) == list: for j, element_text in enumerate(elements_text): if element_text != text: num += 1 elif element_text == text and i < max_times - 1: self.sleep(delay) break else: logger.error('Matching Failed!\nTarget : {} \nActual : {}'.format(text, element_text)) raise WebDriverException('Matching Failed!\nTarget : {} \nActual : {}'.format(text, element_text)) if num == len(elements_text): logger.info('Matching success!') break elif type(elements_text) == str: if elements_text != text: logger.info('Matching success!') break elif i < max_times-1: self.sleep(delay) else: logger.error('Matching Failed!\nTarget : {} \nActual : {}'.format(text, elements_text)) raise WebDriverException('Matching Failed!\nTarget : {} \nActual : {}'.format(text, elements_text)) else: logger.warn('Entered the wrong value of text, please check the value of it !\ntext type:{}\ntext value:{}'.format(type(text),text)) raise Exception('Entered the wrong value of text, please check the value of it !\ntext type:{}\ntext value:{}'.format(type(text),text))
def __init__(self, driver_wrapper, msg): try: if not isinstance(msg, str): msg = 'No message' msg = 'Error executing Javascript: ' + msg finally: WebDriverException.__init__(self, msg)
from scrapy import Selector from selenium import webdriver from selenium.common.exceptions import WebDriverException from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.wait import WebDriverWait from simi.logger.my_logger import MyLogger driver = webdriver.PhantomJS() # driver.implicitly_wait(30) driver.get("http://www.hotstar.com/channels/life-ok") # WebDriverWait(driver, 10, 0.5, WebDriverException(msg='page not load complete')).until( # lambda driver: driver.execute_script('return document.readyState') == 'complete') WebDriverWait(driver, 10, 1, WebDriverException(msg='page not load complete')).until( expected_conditions.presence_of_element_located((By.CLASS_NAME, "resClass"))) # time.sleep(3) driver.execute_script('window.scrollTo(0,1000000)') body = driver.page_source s = Selector(text=body) print s.xpath("//div[@class='resClass']/div/span/article/a/@href").extract() my_logger = MyLogger(__file__[:-3]) my_logger.debug('debug sth') print 'stdout sth' # screenshot driver.get_screenshot_as_file('%s.png' % time.strftime("%y%m%d_%H%M%S"))
def set_viewport_size(driver, required_size): # type: (AnyWebDriver, ViewPort) -> None logger.debug("set_viewport_size({})".format(str(required_size))) actual_viewport_size = get_viewport_size(driver) if actual_viewport_size == required_size: logger.info("Required size already set.") return None try: # We move the window to (0,0) to have the best chance to be able to # set the viewport size as requested. driver.set_window_position(0, 0) except WebDriverException: logger.warning("Failed to move the browser window to (0,0)") set_browser_size_by_viewport_size(driver, actual_viewport_size, required_size) actual_viewport_size = get_viewport_size(driver) if actual_viewport_size == required_size: return None # Additional attempt. This Solves the "maximized browser" bug # (border size for maximized browser sometimes different than # non-maximized, so the original browser size calculation is # wrong). logger.info("Trying workaround for maximization...") set_browser_size_by_viewport_size(driver, actual_viewport_size, required_size) actual_viewport_size = get_viewport_size(driver) logger.debug("Current viewport size: {}".format(actual_viewport_size)) if actual_viewport_size == required_size: return None width_diff = abs(actual_viewport_size["width"] - required_size["width"]) width_step = -1 if width_diff > 0 else 1 # -1 for smaller size, 1 for larger height_diff = abs(actual_viewport_size["height"] - required_size["height"]) height_step = -1 if height_diff > 0 else 1 browser_size = get_window_size(driver) curr_width_change = 0 curr_height_change = 0 if width_diff <= _MAX_DIFF and height_diff <= _MAX_DIFF: logger.info("Trying workaround for zoom...") last_required_browser_size = None while (abs(curr_width_change) <= width_diff and abs(curr_height_change) <= height_diff): if abs(curr_width_change) <= width_diff: curr_width_change += width_step if abs(curr_height_change) <= height_diff: curr_height_change += height_step required_browser_size = dict( width=browser_size["width"] + curr_width_change, height=browser_size["height"] + curr_height_change, ) if required_browser_size == last_required_browser_size: logger.info( "Browser size is as required but viewport size does not match!" ) logger.info("Browser size: {}, Viewport size: {}".format( required_browser_size, actual_viewport_size)) logger.info("Stopping viewport size attempts.") break set_browser_size(driver, required_browser_size) last_required_browser_size = required_browser_size actual_viewport_size = get_viewport_size(driver) logger.info( "Current viewport size: {}".format(actual_viewport_size)) if actual_viewport_size == required_size: return None else: logger.info("Zoom workaround failed.") raise WebDriverException("Failed to set the viewport size.")
from selenium.webdriver import Chrome from selenium.webdriver import Firefox from selenium.webdriver import Safari from selenium.webdriver.common.by import By from selenium.common.exceptions import WebDriverException import config if config.browser == 'Chrome': driver_class = Chrome elif config.browser == 'Safari': driver_class = Safari elif config.browser == 'Firefox': driver_class = Firefox else: raise WebDriverException("No browser specified") implicit_timeout = 5 wait_timeout = 50 default_search_type = By.XPATH
def native_events_enabled(self, value): if value not in [True, False]: raise WebDriverException("Please pass in a Boolean to this call") self.set_preference("webdriver_enable_native_events", value)
def driver_mock(): driver = mock.Mock() driver.desired_capabilities = {"platformName": ""} # need to configure below driver.execute_script = mock.MagicMock(side_effect=WebDriverException()) return driver
def to_right_of(self, element_or_locator): if element_or_locator is None: raise WebDriverException("Element or locator must be given when calling above method") self.filters.append({"kind": "right", "args": [element_or_locator]}) return self
def to_site(self): if self.traffic_source == 'organic': engines = { 'google': ['', '', 'https://www.google.', '//h3[@class="r"]', 'pnnext'], 'yahoo': [ 'https://search.yahoo.com/', 'sbq', 'https://search.yahoo.', '//h3', 'next' ], 'bing': [ 'https://www.bing.com/', 'b_searchbox', 'https://www.bing.', '//h2', 'sb_pagN' ], 'aol': [ 'http://search.aol.com/', 'q', 'http://search.aol.', '//h3', 'gspPageNext' ] } e = engines[self.organic_engine] name = self.organic_engine if not self.settings[0]: self.logger.error('organic settings are empty') return False query = choice(self.settings) self.logger.debug('using {}, query: {}'.format(name, query)) if name == 'google': query_url = 'https://www.google.com/search?q={}&ie=utf-8&oe=utf-8&aq=t'.format( query) query_url = query_url.replace(' ', '+') self.logger.debug('query_url: {}'.format(query_url)) self.open_url(query_url) elif name == 'aol': self.open_url(e[0]) time.sleep(2) inputq = self.br.find_element_by_name(e[1]) ActionChains(self.br).move_to_element(inputq).send_keys( query, Keys.RETURN).perform() results = self.br.find_elements_by_xpath(e[3]) if not results: try: self.br.find_element_by_class_name('csbb').click() except: pass else: self.open_url(e[0]) time.sleep(2) self.br.find_element_by_class_name(e[1]).send_keys( query, Keys.RETURN) time.sleep(1) if not self.br.current_url.startswith(e[2]): if self.br.current_url == 'about:blank': # self.proxy.delete_instance() self.logger.error( 'Cannot open {} through proxy {}, aborting'.format( name, self.proxy)) return False self.logger.error( 'loading url "{}" instead of {}, aborting'.format( self.br.current_url, name)) return False results = None found = False page_counter = 0 while not found: results = self.br.find_elements_by_xpath(e[3]) if not results: raise WebDriverException('something wrong, see screenshot') for result in results: try: a = result.find_element_by_xpath('.//a') except: continue link = a.get_attribute('href') if self.netloc in link: found = True self.logger.debug(u'url found, header: {}'.format( a.text)) a.click() break if page_counter == 4 and not found: self.logger.error('URL {} not found'.format(self.url)) return False if not found: self.logger.debug( 'cannot find url on page {}'.format(page_counter)) if page_counter == 0: timestamp = datetime.datetime.now().strftime( "%Y-%m-%dT%H:%M") if not os.path.exists('screenshots'): os.makedirs('screenshots') scr_path = os.path.join( 'screenshots', 'error_screenshot_' + timestamp + '.png') self.br.save_screenshot(scr_path.replace(':', '_')) self.logger.debug('see screenshot') page_counter += 1 time.sleep(1) if name == 'aol': span = self.br.find_element_by_class_name(e[4]) span.find_element_by_xpath('.//a').click() elif name == 'google': self.br.find_element_by_id(e[4]).click() else: self.br.find_element_by_class_name(e[4]).click() time.sleep(1) if len(self.br.window_handles) > 1: site_tab = self.br.window_handles[1] self.br.close() # close engine tab self.br.switch_to_window(site_tab) elif self.traffic_source == 'direct': self.open_url(self.url) time.sleep(1) if not self.url in self.br.current_url: self.logger.error( 'loading url "{}" instead of site url, aborting'.format( self.br.current_url)) return False elif self.traffic_source == 'social': if not self.settings[0]: self.logger.error('social settings are empty') return False post = choice(self.settings).strip().split('|') post = [p.strip() for p in post] self.logger.debug('post: {}'.format(post)) if post[0] == 'FB': post_found = False self.open_url(post[1]) time.sleep(1) if not 'facebook' in self.br.current_url: self.logger.error('cannot open facebook') return False for article in self.br.find_elements_by_class_name( 'userContentWrapper'): if article.find_element_by_class_name( 'userContent').text.startswith(post[2]): post_found = True link_found = False self.logger.info('post found') for a in article.find_elements_by_xpath('.//a'): href = a.get_attribute('href') if href and self.netloc in href: link_found = True self.logger.info('link found') try: a.click() except: a.click() time.sleep(3) self.br.switch_to_window( self.br.window_handles[1]) # close other tabs cwh = self.br.current_window_handle for tab in self.br.window_handles: if cwh != tab: self.br.switch_to_window(tab) self.br.close() self.br.switch_to_window(cwh) return True if not link_found: self.logger.error( 'cannot find link to site in post {}'.format( post)) return False if not post_found: self.logger.error('cannot find post {}'.format(post)) return False elif post[0] == 'TW': self.open_url(post[1]) time.sleep(1) if not 'twitter' in self.br.current_url: self.logger.error('cannot open twitter') return False link_found = False tweet = self.br.find_element_by_class_name('tweet-text') a = tweet.find_element_by_xpath('.//a') self.logger.info('url in twitter post: {}'.format( a.get_attribute('href'))) a.click() time.sleep(3) self.br.switch_to_window(self.br.window_handles[1]) # close other tabs cwh = self.br.current_window_handle for tab in self.br.window_handles: if cwh != tab: self.br.switch_to_window(tab) self.br.close() self.br.switch_to_window(cwh) return True elif self.traffic_source == 'referral': if not self.settings[0]: self.logger.error('referrer settings are empty') return False self.open_url(choice(self.settings)) time.sleep(3) link_found = False for a in self.br.find_elements_by_xpath('//a'): href = a.get_attribute('href') if href and self.netloc in href: link_found = True a.click() return True if not link_found: self.logger.info('cannot find url on referrer site') return False elif self.traffic_source == 'email': self.logger.debug('email url {}'.format(self.settings)) self.open_url(self.settings) return True
def promise_check_func(): """ Check function which continuously fails with a WebDriverException until timeout """ raise WebDriverException('Boom!')
chromedriver_path += get_os(filename=defaults_filename).lower() browser = "chrome" if args.brave: browser = "brave" if not args.debug: sys.excepthook = exception_handler vid_type = 'movie' if args.show or args.type == 'show': vid_type = 'show' open_streaming_service(vid_type, args.name.strip( ), browser=browser, chromedriver_path=chromedriver_path) except KeyboardInterrupt: print("\nShutting down...") except WebDriverException as e: msg = e.msg if "chromedriver" in e.msg: msg = "Incorrect Operating System chosen. Please reconfigure " \ "it using the \"-c\" or \"--config-os\" token." if "--user-data-dir" in e.msg: msg = "Cannot run multiple instances of Chrome at once. " \ "Please close all your browser tabs launched with this program " \ "before running the program again." raise WebDriverException(msg=msg)
def named_browser(context, name): single_browser = hasattr(context, 'single_browser') if single_browser and hasattr(context, 'browser') and context.browser == name: # don't start up multiple browsers return if name not in context.browsers: args = context.browser_args.copy() if context.remote_webdriver: args['driver_name'] = 'remote' if context.default_browser: args['browser'] = context.default_browser elif context.default_browser: args['driver_name'] = context.default_browser if context.default_browser == 'electron': assert context.electron_app, u'You need to set the electron app path' args['binary'] = context.electron_app if context.default_browser == 'ios': caps = {} assert context.ios_app, u'You need to specify the iOS app' try: caps = context.ios_capabilities except AttributeError: pass try: caps.update(context.personas[name]['ios_capabilities']) except KeyError: pass app_path = context.ios_app args['app_path'] = app_path args['caps'] = caps context.browsers[name] = Browser(**args) elif context.default_browser == 'android': caps = {} assert context.android_app, u'You need to specify the android app' try: caps = context.android_capabilities except AttributeError: pass try: caps.update(context.personas[name]['android_capabilities']) except KeyError: pass app_path = context.android_app args['app_path'] = app_path args['caps'] = caps context.browsers[name] = Browser(**args) else: browser_attempts = 0 while browser_attempts < context.max_browser_attempts: try: context.browsers[name] = Browser(**args) break except WebDriverException: browser_attempts += 1 else: raise WebDriverException("Failed to initialize browser") if context.default_browser_size: context.browsers[name].driver.set_window_size( *context.default_browser_size) context.browser = context.browsers[name] if single_browser: context.is_connected = True
def assert_process_still_running(self): return_code = self.process.poll() if return_code is not None: raise WebDriverException( 'Service %s unexpectedly exited. Status code was: %s' % (self.path, return_code))
executable_path = os.path.join(settings.DRIVER_DIR, 'chromedriver.exe') options = chrome.create_options() chrome.create_driver(options=options) mock_Chrome.assert_called_with(executable_path=executable_path, options=options) @mock.patch('robot.browser.chrome.create_options') @mock.patch('selenium.webdriver.Chrome') def test_create_options_called_by_default(mock_Chrome, mock_create_options): chrome.create_driver() mock_create_options.assert_called() @mock.patch('selenium.webdriver.Chrome', side_effect=WebDriverException('ANY MESSAGE')) def test_avoids_None_in_WebDriverException(mock_Chrome): try: driver = chrome.create_driver() assert driver, 'returns None if a WebDriverException occurs.' except WebDriverException: pass except WebDriverError: pass @mock.patch('selenium.webdriver.Chrome', side_effect=WebDriverException('ANY MESSAGE')) def test_WebDriverException_launches_WebDriverError(mock_Chrome): try: chrome.create_driver()