Python selenium.common.exceptions 模块,TimeoutException() 实例源码
我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用selenium.common.exceptions.TimeoutException()。
def login():
"""??????,??????????"""
driver.get(start_url)
wait = webdriverwait(driver,10)
try:
username = wait.until(EC.presence_of_element_located((By.ID,"loginname")))
password = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="pl_login_form"]/div/div[3]/div[2]/div/input')))
username.send_keys(user)
password.send_keys(passwd)
btn = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="pl_login_form"]/div/div[3]/div[6]/a/span')))
btn.click()
except TimeoutException:
print ("??")
return
except NoSuchElementException:
print ("?????? ")
return
def get_position(self):
"""
???????
:return: ???????
"""
try:
img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'patt-shadow')))
except TimeoutException:
print('??????')
self.open()
time.sleep(2)
location = img.location
size = img.size
top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
'width']
return (top, right)
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 testGetopenRedirect(url: str, cookies: Mapping[str, str]) -> Optional[str]:
""" If the given URL redirects when accessed with the given cookies via GET,return the new URL,otherwise
return None """
driver = SeleniumDrivers.getFirefoxDriver()
driver.setCookies(url, cookies)
try:
driver.get(url)
time.sleep(config.timeout)
if driver.current_url == url:
driver.reset()
return None
else:
url = driver.current_url
driver.reset()
return url
except (TimeoutException, URLError):
driver.reset()
return None
def testPOSTOpenRedirect(url: str, str], data: Mapping[str, str]) -> Optional[str]:
""" If the given URL redirects when accessed with the given cookies via POST, cookies)
try:
driver.post(url, data)
time.sleep(config.timeout)
if driver.current_url == url:
driver.reset()
return None
else:
url = driver.current_url
driver.reset()
return url
except (TimeoutException, URLError):
driver.reset()
return None
def testGETXSSDriver(url: str, driver: webdriver) -> Optional[str]:
""" If the given URL pops an alert Box when accessed with the given cookies,return the contents of the alert Box,
otherwise return None """
driver.setCookies(url, cookies)
try:
driver.get(url)
webdriverwait(driver, config.timeout).until(expected_conditions.alert_is_present())
# Note that despite the name switch_to_alert also handles prompt:
# - http://selenium-python.readthedocs.io/navigating.html#popup-dialogs
alert = driver.switch_to_alert()
text = alert.text
driver.reset()
return text
except (TimeoutException, URLError):
driver.reset()
return None
def testPOSTXSSDriver(url: str, driver: webdriver) -> \
Optional[str]:
""" If the given URL pops an alert Box when accessed with the given cookies, cookies)
try:
driver.post(url, data)
webdriverwait(driver, URLError):
driver.reset()
return None
def retry(func: Callable[[], T]) -> T:
""" Retry the function with 30 second timeouts until it works
- I've observed the getFirefoxDriver() without this freeze once (out of hundreds of runs...) so adding this
as a safety measure. """
for i in range(10):
if config.DEBUG and i > 0:
print("Retry #%s" % str(i))
def timeoutHandler(signum, frame):
raise TimeoutException("Timeout!")
signal.signal(signal.SIgalRM, timeoutHandler)
signal.alarm(delayTime)
try:
t = func()
signal.alarm(0)
return t
except TimeoutException:
pass
signal.alarm(0)
raise TimeoutException("Retried 10 times... Failed!")
def validateLink(self, url, expectedText = None, expectedTitle = None, expectedUrl = None, \
xpathContext = None):
xpathContext = xpathContext if xpathContext else ""
selector = ".//{1}a[@href='{0}']".format(url, xpathContext)
if expectedText:
self.assertTextPresent(By.XPATH, selector, expectedText)
totalTries = self._followLinkMaxRetries + 1
for i in range(0, totalTries):
try:
self.click(By.XPATH, "Click {0}".format(expectedText), \
expectedURL = expectedUrl if expectedUrl else url)
break
except TimeoutException:
retry = i + 1 # First 'try' is not a retry
if retry < totalTries:
pass
if expectedTitle:
self.assertTitle(expectedTitle);
def until(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value is not False."""
screen = None
stacktrace = None
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message, screen, stacktrace)
def wait_until_element_is_clickable(self, browser_instance, locator_type, locator,
timeout=5):
try:
webdriverwait(browser_instance, int(timeout)).until(EC.element_to_be_clickable((BYCLASS[locator_type.strip().upper()], locator)))
status = True
except KeyError:
print_error("The given locator_type - '{0}' does not match any of "
"the accepted locator_types.".format(locator_type))
print_error("{0},{1},{2},{3},{4},{5},{6},and {7} are the "
"accepted locator types.".format("id", "xpath", "link"
"class", "tag", "name",
"css_selector",
"partial_link"))
status = "ERROR"
except TimeoutException:
print_error("Element unclickable after {0} seconds".format(timeout))
status = False
except Exception as e:
print_error("An Exception Ocurred: {0}".format(e))
status = "ERROR"
return status
def wait_until_presence_of_element_located(self,
locator_type, timeout=5):
try:
webdriverwait(browser_instance, int(timeout)).until(EC.presence_of_element_located((BYCLASS[locator_type.strip().upper()],
"partial_link"))
status = "ERROR"
except TimeoutException:
print_error("Element not present after {0} seconds".format(timeout))
status = False
except Exception as e:
print_error("An Exception Ocurred: {0}".format(e))
status = "ERROR"
return status
def wait_until_presence_of_all_elements_located(self,
locator_type, int(timeout)).until(EC.presence_of_all_elements_located((BYCLASS[locator_type.strip().upper()],
"partial_link"))
status = "ERROR"
except TimeoutException:
print_error("Elements not present after {0} seconds".format(timeout))
status = False
except Exception as e:
print_error("An Exception Ocurred: {0}".format(e))
status = "ERROR"
return status
def wait_until_visibility_of_element_located(self,
locator_type,
timeout=5):
try:
webdriverwait(browser_instance, int(timeout)).until(EC.visibility_of_element_located((BYCLASS[locator_type.strip().upper()],
"partial_link"))
status = "ERROR"
except TimeoutException:
print_error("Element not visible after {0} seconds".format(timeout))
status = False
except Exception as e:
print_error("An Exception Ocurred: {0}".format(e))
status = "ERROR"
return status
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, 'submit-response')))
textarea = webdriverwait(self.driver, '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 setUp(self):
self.driver = self.getDriver()
print("Got driver")
self.driver.get(os.environ["OS_HORIZON"])
try:
delay = 5
element_present = EC.presence_of_element_located((By.NAME,
'username'))
webdriverwait(self.driver, delay).until(element_present)
self.driver.title.index("Login - OpenStack Dashboard")
elem = self.driver.find_element_by_name("username")
elem.send_keys(os.environ["OS_USERNAME"])
elem = self.driver.find_element_by_name("password")
elem.send_keys(os.environ["OS_PASSWORD"])
elem.send_keys(Keys.RETURN)
except TimeoutException:
print("Loading took too much time!")
raise
time.sleep(2)
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.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 until_not(self, message=''):
"""
Calls the method provided with the driver as an argument until the \
return value is False.
"""
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if not value:
return value
except self._ignored_exceptions:
return True
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message)
def wait_activity(self, activity, timeout, interval=1):
"""Wait for an activity: block until target activity presents
or time out.
This is an Android-only method.
:Agrs:
- activity - target activity
- timeout - max wait time,in seconds
- interval - sleep interval between retries,in seconds
"""
try:
webdriverwait(self, interval).until(
lambda d: d.current_activity == activity)
return True
except TimeoutException:
return False
def get_info(self,id):
'''???????????????????????????????'''
dic = {}
url = 'https://detail.tmall.hk/hk/item.htm?spm=a220o.1000855.1998025129.2.102f616emYvWTL&abtest=_AB-LR32-PR32&pvid=a2be15e3-8200-46b3-bd09-de041b8b8dc3&pos=2&abbucket=_AB-M32_B9&acm=03054.1003.1.2431317&id={}&scm=1007.12144.81309.23864_0&sku_properties=-1:-1'.format(id)
self.driver.get(url)
# html = self.driver.page_source
# print(html)
try:
location = self.waiter.until(
EC.presence_of_element_located((By.XPATH,'//dl[@class="tm-happy11-panel"]/dd/span[@class="tm-price"]'))
)
info = location.text.strip()
dic['id'] = id
dic['price'] = info if info else '?????'
self.write_info(dic)
except TimeoutException as e:
print(e)
dic['id'] = id
dic['price'] = '{}????????'.format(e).strip()
self.write_info(dic)
def wait_for_alert(t=3):
"""Wait for an alert Box to be present
Arguments:
t -- the timeout in seconds (default = 3)
Returns:
The alert Selenium object or False
"""
if verbose:
print 'Waiting for alert to pop...'
try:
webdriverwait(browser, t).until(EC.alert_is_present())
return browser.switch_to_alert()
except TimeoutException:
return False
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 search_mainpage(query,times=0):
try:
baseurl = 'http://www.tianyancha.com/search?'
params = {
'key':query,
'checkForm':'searchBox'
}
url = baseurl + urllib.parse.urlencode(params)
print (url)
driver.get(url)
wait = webdriverwait(driver, 10)
try:
href = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ng-view"]/div[2]/div/div/div[1]/div[3]/div[1]/div[2]/div[1]/div[1]/a'))).get_attribute('href')
except Exception as e:
print ('??????')
href = ''
return href
except TimeoutException:
print ('crawl again')
if times < 5:
times+=1
search_mainpage(query,times)
else:
return ''
def login_by_cookie(browser):
print '????'
try:
browser.get('http://weibo.com/')
except TimeoutException:
print '???????????'
browser.execute_script('window.stop()')
browser.execute_script('window.stop()')
print('????')
time.sleep(5)
browser.execute_script('window.stop()')
browser.delete_all_cookies()
print('????cookies')
for cookie in pickle.load(open('cookie')):
try:
browser.add_cookie(cookie)
except:
pass
print('????cookies')
browser.get('http://m.weibo.cn/mblog')
time.sleep(5)
print '????:'
print browser.title
if '????' in browser.title.encode('utf-8'):
pickle.dump(browser.get_cookies(), open('cookie', 'wb'))
def check_captcha(driver, condition):
""" Check if it expects a captcha """
captcha = driver.find_elements_by_css_selector("#captcha")
captcha += driver.find_elements_by_css_selector("#gs_captcha_f")
captcha += driver.find_elements_by_css_selector("#g-recaptcha")
captcha += driver.find_elements_by_css_selector("#recaptcha")
while captcha:
print("Ops. It requires a captcha!")
print("If you filled in the browser,type '<ok>' here.")
inp = input("Captcha: ")
if inp == "<ok>":
break
captcha[0].send_keys(inp)
captcha[0].send_keys(Keys.RETURN)
try:
if condition:
wait_for(driver, condition)
break
except TimeoutException:
captcha = driver.find_elements_by_css_selector("#captcha")
def get_position(self):
"""
???????
:return: ???????
"""
try:
img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, right)
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 next_page(page_number):
print("---????: " + str(page_number))
try:
input = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#J_bottomPage > span.p-skip > input"))
)
submit = wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "#J_bottomPage > span.p-skip > a"))
)
input.clear()
input.send_keys(page_number)
submit.click()
wait.until(
EC.text_to_be_present_in_element(
(By.CSS_SELECTOR, '#J_bottomPage > span.p-num > a.curr'), str(page_number))
)
get_products()
except TimeoutException:
next_page(page_number)
def search():
print("---?????: " + KEY_WORD_TAOBAO)
print('os.path: ' + str(os.path))
try:
browser.get('https://www.taobao.com')
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#q')))
submit = wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_TSearchForm > div.search-button > button'))
)
input.send_keys(KEY_WORD_TAOBAO)
submit.click()
total = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.total'))
)
get_products()
return total.text
except TimeoutException:
return search()
def repeat_next(self):
while True:
try:
next_button = self.wait.until(
EC.element_to_be_clickable(
(By.CLASS_NAME, 'float-right'))
)
pages = self.driver.find_elements(By.CSS_SELECTOR, '.current.reference.internal')
for page in pages:
("\n" + page.get_attribute('text'))
self.click(next_button)
except NoSuchElementException:
print("End of page")
break
except TimeoutException:
print("Timeout error...")
break
def click_all_button(self):
""" click the button that redirects to the list of all webcams
Raises:
UnexpecteDalertPresentException: it occurs when a ramdom alert dialog pops up
TimeoutException: it occurs when the element we want is never loaded
"""
try:
# wait until the element is loaded
self.wait.until(lambda driver: driver.find_element_by_id("VERZEICHNIS-button_alle"))
# get the button element and click it
button = self.driver.find_element_by_id("VERZEICHNIS-button_alle")
button.click()
except UnexpecteDalertPresentException:
self.handle_alert()
self.click_all_button()
except:
self.driver.refresh()
self.click_all_button()
def get_index():
try:
print('????')
browser.get('https://www.lagou.com/')
close_window = wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '#cBoxClose'))
)
close_window.click()
time.sleep(2)
search_place = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '#search_input'))
)
search_button = wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '#search_button'))
)
search_place.send_keys(WORK)
time.sleep(2)
search_button.click()
except TimeoutException as e:
print('?????')
login()
def scrapeLocation(self, location, dateto, dateFrom, maxPosts):
"""return number of posts at location since dateFrom till dateto,scrolling at most to maxPosts"""
self.timeLimit = datetime.Now() + timedelta(minutes=TIMEOUT_MINUTES)
attempts = 0
while(attempts < 3): #have to restart scrape if clicked on deleted post
if(attempts > 0):
print("\nrestarting scrape of " + location + ",attempt " + str(attempts) + "\n")
try:
self.browseLocationPage(location)
try:
postList = self.scrollToDate(dateFrom, maxPosts)
if(len(postList) <= 9): #no recent posts,only "top posts" or none
return 0
firstPostIndex = self.findFirstPost(dateFrom, postList)
lastPostIndex = self.findLastPost(dateto, postList, firstPostIndex)
except TimeoutException:
print("scraper " + location + " timed out")
return ERROR_INDICATOR
if(len(postList) == (maxPosts + 9)): #didn't finish scrolling
return -(firstPostIndex - lastPostIndex) #negative = more than
else:
return (firstPostIndex - lastPostIndex)
except self.PageNotFoundException:
attempts += 1
def Logged(twitchUsername, twitchPassword):
global oldProxy
#checks if logged
try:
webdriverwait(driver, delay - 5).until(EC.presence_of_element_located((By.ID, searchFieldID)))
Follow()
except TimeoutException:
try:
driver.find_element_by_xpath(incorrectCapthaXpath)
print ("Incorrect CAPTCHA,changing proxy!!!" + '\n')
print("------------------------------------------- " + '\n')
change_proxy(twitchUsername, twitchPassword)
except:
try:
driver.find_element_by_xpath(incorrectUserXpath)
print ("Incorrect username or password,changing account!!!" + '\n')
print("------------------------------------------- " + '\n')
oldProxy = proxies[proxy_count]
change_Account = True
driver.quit()
except:
print ("Time expired,changing proxy!!!(2)" + '\n')
print("------------------------------------------- " + '\n')
change_proxy(twitchUsername, twitchPassword)
def crawl(timeout=7, n_urls=len(top500.urls), start=0):
logger.info('starting new crawl with timeout %s n_urls %s start %s' % (timeout, n_urls, start))
with xvfb_manager():
driver = start_driver()
driver.set_page_load_timeout(timeout)
driver.set_script_timeout(timeout)
for url in top500.urls[start:start+n_urls]:
try:
logger.info('visiting %s' % url)
driver.get(url)
sleep(timeout)
except TimeoutException as e:
logger.info('timeout on %s ' % url)
driver = timeout_workaround(driver)
continue
data = dump_data(driver)
driver.quit()
return data
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 clk(driver, url):
"""locate and click url"""
locator = (By.XPATH, '//span[contains(text(),"{0}")]/../../../../a'.format(url))
try:
webdriverwait(driver, 5, 0.5).until(EC.element_to_be_clickable(locator))
try:
moved_to_element = driver.find_element_by_xpath('//span[contains(text(),"{0}")]'.format(url))
target_element = driver.find_element(*locator)
ActionChains(driver).move_to_element(to_element=moved_to_element).click(target_element).perform()
sleep(1)
if 'm.baidu.com' in driver.current_url:
target_element.click()
return 1
except:
return 0
except TimeoutException:
return 0
def wait_exists(self, timeout=None):
"""
Wait for the element to appear in the DOM.
Args:
timeout:
None or int
Returns:
self
"""
wait_time = 0
while not self.exists():
sleep(1)
wait_time += 1
if timeout is not None:
if wait_time >= timeout:
raise TimeoutException()
return self
def get_by_selenium():
url = 'https://www.youtube.com/channel/UC3qWcF49rv8VMZO7Vg6kj5w'
cap = webdriver.DesiredCapabilities.PHANTOMJS
cap["phantomjs.page.settings.resourceTimeout"] = 1000
cap["phantomjs.page.settings.loadImages"] = False
cap["phantomjs.page.settings.localToRemoteUrlAccessEnabled"] = True
cap["userAgent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"
cap["XSSAuditingEnabled"] = True
driver = webdriver.PhantomJS(desired_capabilities=cap,
service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any',
'--web-security=true'])
# driver = webdriver.Chrome()
driver.get(url)
wait = webdriverwait(driver, 20)
# print driver.page_source
try:
fans = int(wait.until(lambda x: x.find_elements_by_class_name('yt-subscription-button-subscriber-count-branded-horizontal')[0].text))
except TimeoutException:
fans = 0
print fans
driver.quit()
return fans
def get_by_selenium():
url = 'http://i.youku.com/i/UMjk1ODg3NDgwOA=='
cap = webdriver.DesiredCapabilities.PHANTOMJS
cap["phantomjs.page.settings.resourceTimeout"] = 1000
cap["phantomjs.page.settings.loadImages"] = False
cap["phantomjs.page.settings.localToRemoteUrlAccessEnabled"] = True
cap["userAgent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"
cap["XSSAuditingEnabled"] = True
driver = webdriver.PhantomJS(desired_capabilities=cap, 20)
print driver.page_source
try:
fans = int(wait.until(lambda x: x.find_elements_by_class_name('snum')[0].find_element_by_xpath('em').text))
except TimeoutException:
fans = 0
print fans
driver.quit()
return fans
def test_problems_accesible(self):
"""
Tests that at least one problem is accessible from the problems page.
"""
self.driver.get(BASE_URI+"problems")
try:
some_problem_description = self.find_class("problem-description")
except TimeoutException as e:
assert False, "No problems are visible to a new user."
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。