Python selenium.common.exceptions 模块,WebDriverException() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用selenium.common.exceptions.WebDriverException()。
def __init__(self, profilePath, driverPath):
options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
options.add_argument("user-data-dir=" + profilePath)
options.add_argument("headless")
options.add_argument('window-size=1920x1080')
options.add_experimental_option("prefs", prefs)
try:
self.driver = webdriver.Chrome(executable_path=driverPath, chrome_options=options)
except WebDriverException:
print("Failed to start driver at " + driverPath)
self.inUse = True #scraper with no driver is not ready to handle new job
#traceback.print_exc()
self.inUse = False #if False,is ready to handle new job
self.bannerClosed = False #email banner only needs to be closed on first load
self.timeLimit = datetime.Now() + timedelta(minutes=TIMEOUT_MINUTES)
def start(self):
"""
Starts PhantomJS with GhostDriver.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
self.process = subprocess.Popen(self.service_args, stdin=subprocess.PIPE,
close_fds=platform.system() != 'Windows',
stdout=self._log, stderr=self._log)
except Exception as e:
raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
count = 0
while not utils.is_connectable(self.port):
count += 1
time.sleep(1)
if count == 30:
raise WebDriverException("Can not connect to GhostDriver")
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 __init__(self, firefox_profile=None, firefox_binary=None, timeout=30,
desired_capabilities=dc.DesiredCapabilities.FIREFOX,
proxy=None):
try:
if firefox_profile is None:
firefox_profile = firefox.webdriver.FirefoxProfile()
self.setup_profile(firefox_profile)
super(WebDriver, self).__init__(
firefox_profile, FirefoxBinary(), timeout,
desired_capabilities, proxy)
except selenium_exceptions.WebDriverException:
# If we can't start,cleanup profile
shutil.rmtree(self.profile.path)
if self.profile.tempfolder is not None:
shutil.rmtree(self.profile.tempfolder)
raise
def _upload(self, filename):
fp = IOStream()
zipped = zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED)
zipped.write(filename, os.path.split(filename)[1])
zipped.close()
content = base64.encodestring(fp.getvalue())
if not isinstance(content, str):
content = content.decode('utf-8')
try:
return self._execute(Command.UPLOAD_FILE,
{'file': content})['value']
except WebDriverException as e:
if "Unrecognized command: POST" in e.__str__():
return filename
elif "Command not found: POST " in e.__str__():
return filename
elif '{"status":405,"value":["GET","HEAD","DELETE"]}' in e.__str__():
return filename
else:
raise e
def file_detector(self, detector):
"""
Set the file detector to be used when sending keyboard input.
By default,this is set to a file detector that does nothing.
see FileDetector
see LocalFileDetector
see UselessFileDetector
:Args:
- detector: The detector to use. Must not be None.
"""
if detector == None:
raise WebDriverException("You may not set a file detector that is null")
if not isinstance(detector, FileDetector):
raise WebDriverException("Detector has to be instance of FileDetector")
self._file_detector = detector;
def _wait_until_connectable(self, timeout=30):
"""Blocks until the extension is connectable in the firefox."""
count = 0
while not utils.is_connectable(self.profile.port):
if self.process.poll() is not None:
# browser has exited
raise WebDriverException("The browser appears to have exited "
"before we Could connect. If you specified a log_file in "
"the FirefoxBinary constructor,check it for details.")
if count >= timeout:
self.kill()
raise WebDriverException("Can't load the profile. Profile "
"Dir: %s If you specified a log_file in the "
"FirefoxBinary constructor,check it for details."
% (self.profile.path))
count += 1
time.sleep(1)
return True
def _make_ff(self, webdriver_remote_url, desired_capabilites, profile_dir,
binary, gecko_path):
"""Create an instance of firefox browser"""
try:
if webdriver_remote_url:
browser = self._create_remote_web_driver(
webdriver.DesiredCapabilities.FIREFOX,
webdriver_remote_url,
profile_dir)
else:
ff_capabilities = webdriver.DesiredCapabilities.FIREFOX
if ff_capabilities['marionette']:
ff_capabilities['acceptInsecureCerts'] = True
ffbinary = FirefoxBinary(binary)
browser = webdriver.Firefox(firefox_binary=ffbinary,
firefox_profile=profile_dir,
executable_path=gecko_path)
else:
browser = webdriver.Firefox(firefox_profile=profile_dir)
return browser
except WebDriverException as e:
if "executable needs to be in PATH" in str(e):
print_error("Please provide path for geckodriver executable")
elif "Expected browser binary location" in str(e):
print_error("Please provide path of firefox executable")
def _generic_make_browser(self, webdriver_type, desired_cap_type,
webdriver_remote_url, desired_caps, binary):
"""most of the make browser functions just call this function which creates the
appropriate web-driver"""
try:
if not webdriver_remote_url:
if binary is not None:
browser = webdriver_type(binary)
else:
browser = webdriver_type()
else:
browser = self._create_remote_web_driver(desired_cap_type,
webdriver_remote_url,
desired_caps)
return browser
except WebDriverException as e:
if "executable needs to be in PATH" in str(e):
print_error("Please provide path for chrome driver executable")
def click_when_loaded(self, by, value, retries=DEFAULT_RETRY_COUNT):
"""
Helper method to tell the driver to wait until an element is loaded,then click it.
Since clicking on page elements is the most common source of test flakiness in our selenium suite,this
includes some functionality for retrying the click some number of times if specific exceptions are raised.
"""
retries_remaining = retries
wait = self.wait()
while True:
try:
return wait.until(
lambda driver: driver.find_element(by, value)
).click()
except WebDriverException:
if retries_remaining > 0:
retries_remaining -= 1
else:
raise
def get_results(self,
url: str,
controller_wait: t.Optional[ControllerWait]=None) -> ControllerUrlResult:
try:
self._webdriver.get(url)
# If we have a specified controller wait,let's wait until the desired state is reached
# before auditing states and validators
if controller_wait:
try:
controller_wait.wait_until()
except ControllerWaitTimeout:
logger.warning("ControllerWait state not reached for {}".format(url))
# Perform our auditors -- also block until certain states are reached
state_results = self._loaded_state_manager.get_state_results(self._webdriver)
# After our the page reaches a testable state,Now let's run all our validators on it
# Todo: Consider dependant graphs for validators based on states
validator_results = self._validator_manager.validate(self._webdriver)
except selenium_exceptions.WebDriverException as e:
logging.exception("Failed result attempt for {}".format(url))
raise ControllerResultsFailed(e)
return ControllerUrlResult(state_results, validator_results)
def getDriver(self):
count = 0
while(True):
try:
ce = "http://%s:4444/wd/hub" % os.environ["HUB"]
driver = webdriver.Remote(
command_executor=ce,
desired_capabilities={
"browserName": os.environ.get("browser", "firefox"),
"platform": "Linux"
}
)
return driver
except WebDriverException as e:
s = "%s" % e
print("Got exception %s" % s)
print("%s" % dir(s))
if "Empty pool of VM for setup Capabilities" not in s:
raise
time.sleep(5)
if count == 60:
raise Exception("Time out trying to get a browser")
count += 1
def _upload(self,"DELETE"]}' in e.__str__():
return filename
else:
raise e
def file_detector(self, FileDetector):
raise WebDriverException("Detector has to be instance of FileDetector")
self._file_detector = detector;
def start(self):
"""
Starts the OperaDriver Service.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
self.process = subprocess.Popen(["java", "-jar", self.path, "-port", "%s" % self.port])
except:
raise WebDriverException(
"OperaDriver executable needs to be available in the path.")
time.sleep(10)
count = 0
while not utils.is_connectable(self.port):
count += 1
time.sleep(1)
if count == 30:
raise WebDriverException("Can not connect to the OperaDriver")
def _wait_until_connectable(self):
"""Blocks until the extension is connectable in the firefox."""
count = 0
while not utils.is_connectable(self.profile.port):
if self.process.poll() is not None:
# browser has exited
raise WebDriverException("The browser appears to have exited "
"before we Could connect. If you specified a log_file in "
"the FirefoxBinary constructor,check it for details.")
if count == 30:
self.kill()
raise WebDriverException("Can't load the profile. Profile "
"Dir: %s If you specified a log_file in the "
"FirefoxBinary constructor,check it for details.")
count += 1
time.sleep(1)
return True
def get_property(self, name):
"""
Gets the given property of the element.
:Args:
- name - Name of the property to retrieve.
Example::
# Check if the "active" CSS class is applied to an element.
text_length = target_element.get_property("text_length")
"""
try:
return self._execute(Command.GET_ELEMENT_PROPERTY, {"name": name})["value"]
except WebDriverException:
# if we hit an end point that doesnt understand getElementProperty lets fake it
return self.parent.execute_script('return arguments[0][arguments[1]]', self, name)
def _upload(self, {'file': content})['value']
except WebDriverException as e:
if "Unrecognized command: POST" in e.__str__():
return filename
elif "Command not found: POST " in e.__str__():
return filename
elif '{"status":405,"DELETE"]}' in e.__str__():
return filename
else:
raise e
def run(self):
"""??????
????????????????
:return:
"""
while True:
try:
self.scheduler.run(self.browser, self.sqlScanner, self.setting)
break
except WebDriverException:
if execute("ps | awk '{print $4}' | grep firefox"): # still alive or not
self.scheduler.flush()
logger.error(traceback.format_exc())
break
# restart headless browser
self.initbrowser(self.proxy)
except:
logger.error(traceback.format_exc())
self.scheduler.flush()
break
def _upload(self,"DELETE"]}' in e.__str__():
return filename
else:
raise e
def file_detector(self, FileDetector):
raise WebDriverException("Detector has to be instance of FileDetector")
self._file_detector = detector;
def _wait_until_connectable(self,check it for details."
% (self.profile.path))
count += 1
time.sleep(1)
return True
def start_driver(self):
try:
driver = self.find_chromedriver()
except WebDriverException:
logger.error('Unable to locate chromedriver')
time.sleep(0.1)
download = raw_input('\nNo chrome driver found. Download? (y/n): ')
if download.lower().startswith('y'):
self.download_chromedriver()
try:
driver = webdriver.Chrome(executable_path=self.chrome_path)
except WebDriverException as e:
if 'cannot find' in e.msg:
logger.critical('Could not start Chrome browser')
raise SystemExit('Forms log cannot be retrieved without Chrome and chromedriver.')
else:
logger.exception('Cannot start the Chrome browser')
raise SystemExit('Forms log cannot be retrieved without Chrome and chromedriver.')
else:
raise SystemExit('Forms log cannot be retrieved without Chrome and chromedriver.')
return driver
def execute(self):
logging.info('...uruchamianie SeleniumStrategy')
logging.info('...uruchamianie przegl?darki - %s', settings.broWSER)
self.open_browser()
try:
self.driver.get(settings.LOGIN_URL)
self.log_in()
logging.info('...pobieranie numerów id')
ids = self.get_ids()
self.driver.quit()
logging.info('...generowanie wpisów')
return self.get_content_by_ids(ids)
except WebDriverException:
logging.warning('logowanie nie powiod?o si?.')
try:
self.driver.quit()
except WebDriverException:
pass
raise SystemExit
def _upload(self,"DELETE"]}' in e.__str__():
return filename
else:
raise e
def file_detector(self,this is set to a file detector that does nothing.
see FileDetector
see LocalFileDetector
see UselessFileDetector
:Args:
- detector: The detector to use. Must not be None.
"""
if detector is None:
raise WebDriverException("You may not set a file detector that is null")
if not isinstance(detector, FileDetector):
raise WebDriverException("Detector has to be instance of FileDetector")
self._file_detector = detector
def _wait_until_connectable(self, timeout=30):
"""Blocks until the extension is connectable in the firefox."""
count = 0
while not utils.is_connectable(self.profile.port):
if self.process.poll() is not None:
# browser has exited
raise WebDriverException(
"The browser appears to have exited "
"before we Could connect. If you specified a log_file in "
"the FirefoxBinary constructor,check it for details.")
if count >= timeout:
self.kill()
raise WebDriverException(
"Can't load the profile. Profile "
"Dir: %s If you specified a log_file in the "
"FirefoxBinary constructor,check it for details."
% (self.profile.path))
count += 1
time.sleep(1)
return True
def new_chrome_driver(self):
try:
wd = webdriver.Chrome(chrome_options=self.chrome_options) #,service_args=["--verbose","--log-path=/tmp/chromedriver.log"]
except WebDriverException as ex:
if ex.msg.startswith('Unable to either launch or connect to Chrome'):
ex.msg += ' *****NOTE****: On linux,chrome needs write access to /dev/shm.'
ex.msg += ' This is often not the case when you are running inside a *chroot*.'
ex.msg += ' To fix,add the following line in the chroot\'s /etc/fstab: '
ex.msg += ' "tmpfs /dev/shm tmpfs rw,noexec,nosuid,nodev 0 0" '
ex.msg += ' .... and then run sudo mount /dev/shm '
ex.msg += '\n\n ***ALTERNATIVE*** An alternative solution (when using schroot) is'
ex.msg += ' to make sure that /etc/schroot/default/fstab contain a line for /dev/shm.'
ex.msg += ' (usually it is commented out)'
raise
self.reahl_server.install_handler(wd)
return wd
def _upload(self,"DELETE"]}' in e.__str__():
return filename
else:
raise e
def file_detector(self, FileDetector):
raise WebDriverException("Detector has to be instance of FileDetector")
self._file_detector = detector;
def start(self):
"""
Starts PhantomJS with GhostDriver.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
self.process = subprocess.Popen(self.service_args, e)
count = 0
while not utils.is_connectable(self.port):
count += 1
time.sleep(1)
if count == 30:
raise WebDriverException("Can not connect to GhostDriver")
def start(self):
"""
Starts the OperaDriver Service.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
self.process = subprocess.Popen(["java", "%s" % self.port])
except:
raise WebDriverException(
"OperaDriver executable needs to be available in the path.")
time.sleep(10)
count = 0
while not utils.is_connectable(self.port):
count += 1
time.sleep(1)
if count == 30:
raise WebDriverException("Can not connect to the OperaDriver")
def _wait_until_connectable(self):
"""Blocks until the extension is connectable in the firefox."""
count = 0
while not utils.is_connectable(self.profile.port):
if self.process.poll() is not None:
# browser has exited
raise WebDriverException("The browser appears to have exited "
"before we Could connect. If you specified a log_file in "
"the FirefoxBinary constructor,check it for details.")
count += 1
time.sleep(1)
return True
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,
self._get_trace())
except WebDriverException:
driver.implicitly_wait(1)
element = webdriverwait(driver,
self._get_trace())
try:
element.click()
except WebDriverException:
sleep(1)
element = webdriverwait(driver,
self._get_trace())
element.click()
def execute_javascript(self, javascript):
self.install_custom_javascript_functions()
try:
amended_javascript="""window.webnuke = function(){"""+javascript+""";}; webnuke(); return window.console.flushOutput() """
result = self.driver.execute_script(amended_javascript)
if result is not None:
for result_line in result:
print result_line
except WebDriverException:
# ignore any web driver errors
##print "ERROR with webdriver"
#print javascript
#print ''
pass
except:
raise
print ''
def execute_javascript(self, driver, javascript):
self.install_custom_javascript_functions(driver)
try:
amended_javascript="""window.wn_webnuke = function(){"""+javascript+""";}; wn_webnuke(); return window.console.flushOutput() """
result = driver.execute_script(amended_javascript)
if result is not None:
for result_line in result:
print result_line
except WebDriverException as e:
print "ERROR with webdriver: %s"%str(e)
print javascript
print ''
pass
except:
raise
print ''
def start_app_driver(self):
"""This is a docstring"""
try:
while not appiumserver.is_running():
appiumserver.re_start_server()
sleep(1)
else:
if self.driver is None:
self.mutex.acquire()
try:
print "begin start AppDriver"
self.driver = webdriver.Remote(self.baseUrl, self.desired_capabilities)
print "start AppDriver success"
except urllib2.URLError as e:
self.driver = None
print e
self.mutex.release()
return self.driver
except WebDriverException:
raise e
def click_stubborn(driver, e, xpath):
logging.debug("Starting stubborn clicks")
MAX_ATTEMPT = 6
attempt = 0
while attempt < MAX_ATTEMPT:
try:
for i in range(10):
attempt += 1
e.click()
break
# breaks if no exception happens
break
except StaleElementReferenceException:
a_nice_refresh(driver)
e = wait_for_xpath_presence(driver, xpath)
except WebDriverException:
break
return e
def WebElement_click(self):
"""
if element.click() fails with selenium.common.exceptions.WebDriverException,scroll down and try again
"""
try:
WebElement.old_click(self)
except WebDriverException as e:
if "Element is not clickable at point" in str(e):
self.parent.execute_script("arguments[0].scrollIntoView();", self)
WebElement.old_click(self)
else:
# reraise exception after enough tries
self.parent.get_screenshot_as_file('screenshot.png')
raise
# monkey patch the WebElement class
def click_search_url(cls, items):
content = env.threadlocal.broWSER.find_element_by_css_selector("div[id=\"content_left\"]")
links = content.find_elements_by_tag_name("a")
i = 0
for link in links:
if link.get_attribute('class') == "c-showurl":
if i in items:
log.step_normal("????item: [%s]" % i)
log.step_normal("????url?[%s],??????: [%s]" % (link.get_attribute('href'), link.text))
try:
link.click()
random.randint(3, 7)
Webbrowser.SwitchToNewPopWindow()
Webbrowser.SwitchToDefaultwindow()
log.step_normal('?????????')
except (TimeoutException, WebDriverException) as e:
log.step_normal('??????---> %s' % e)
Webbrowser.SwitchToDefaultwindow()
i = i + 1
#??????????????
def rs3topng(rs3_filepath, png_filepath=None):
"""Convert a RS3 file into a PNG image of the RST tree.
If no output filename is given,the PNG image is returned
as a string (which is useful for embedding).
"""
try:
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
except ImportError:
raise ImportError(
'Please install selenium: pip install selenium')
html_str = rs3tohtml(rs3_filepath)
temp = tempfile.NamedTemporaryFile(suffix='.html', delete=False)
temp.write(html_str.encode('utf8'))
temp.close()
try:
driver = webdriver.PhantomJS()
except WebDriverException as err:
raise WebDriverException(
'Please install phantomjs: http://phantomjs.org/\n' + err.msg)
driver.get(temp.name)
os.unlink(temp.name)
png_str = driver.get_screenshot_as_png()
if png_filepath:
with open(png_filepath, 'w') as png_file:
png_file.write(png_str)
else:
return png_str
def _get_Chrome(self):
try:
chrome_ops = webdriver.ChromeOptions()
if self.proxy:
chrome_ops = webdriver.ChromeOptions()
chrome_ops.add_argument(
'--proxy-server={}://{}:{}'.format(
self.proxy.proto,
self.proxy.host,
self.proxy.port
)
)
self.webdriver = webdriver.Chrome(
executable_path=self.config['executable_path'],
chrome_options=chrome_ops
)
chrome_ops.add_argument('--no-sandBox')
chrome_ops.add_argument('--start-maximized')
chrome_ops.add_argument(
'--window-position={},{}'.format(
randint(10, 30),
randint(10, 30)
)
)
chrome_ops.add_argument(
'--window-size={},{}'.format(
randint(800, 1024),
randint(600, 900)
)
)
self.webdriver = webdriver.Chrome(
executable_path=self.config['executable_path'],
chrome_options=chrome_ops
)
return True
except WebDriverException:
raise
return False
def _get_PhantomJS(self):
try:
service_args = []
if self.proxy:
service_args.extend([
'--proxy={}:{}'.format(self.proxy.host, self.proxy.port),
'--proxy-type={}'.format(self.proxy.proto),
])
if self.proxy.username and self.proxy.password:
service_args.append(
'--proxy-auth={}:{}'.format(
self.proxy.username,
self.proxy.password
)
)
useragent = random_user_agent(
mobile=False
)
logger.info('useragent: {}'.format(useragent))
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = useragent
try:
self.webdriver = webdriver.PhantomJS(
executable_path=self.config['executable_path'],
service_args=service_args,
desired_capabilities=dcap
)
return True
except (ConnectionError, ConnectionRefusedError, ConnectionResetError) as err:
logger.error(err)
return False
except WebDriverException as e:
logger.error(e)
return False
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