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
python类WebDriverWait()的实例源码
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)
test_complete_reading_assignment_student.py 文件源码
项目:TestRewrite
作者: osqa-interns
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
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)
test_start_late_homework_student.py 文件源码
项目:TestRewrite
作者: osqa-interns
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
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)
test_open_homework_immediate_student.py 文件源码
项目:TestRewrite
作者: osqa-interns
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
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)
test_assessment errata_form_student.py 文件源码
项目:TestRewrite
作者: osqa-interns
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
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()
test_performance_practice_student.py 文件源码
项目:TestRewrite
作者: osqa-interns
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
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()
test_set_course_details_admin.py 文件源码
项目:TestRewrite
作者: osqa-interns
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
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)))