Python selenium.webdriver.support.expected_conditions.presence_of_all_elements_located() Examples
The following are 25
code examples of selenium.webdriver.support.expected_conditions.presence_of_all_elements_located().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
selenium.webdriver.support.expected_conditions
, or try the search function
.
Example #1
Source File: common.py From bitmask-dev with GNU General Public License v3.0 | 7 votes |
def _wait_until_elements_are_visible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S): wait = WebDriverWait(context.browser, timeout) wait.until(EC.presence_of_all_elements_located(locator_tuple)) return context.browser.find_elements(locator_tuple[0], locator_tuple[1])
Example #2
Source File: wechat_moment.py From learn_python3_spider with MIT License | 6 votes |
def get_data(self): while True: # 获取 ListView items = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.tencent.mm:id/eew'))) # 滑动 self.driver.swipe(self.start_x, self.start_y, self.end_x, self.end_y, 2000) #遍历获取每个List数据 for item in items: moment_text = item.find_element_by_id('com.tencent.mm:id/kt').text day_text = item.find_element_by_id('com.tencent.mm:id/eke').text month_text = item.find_element_by_id('com.tencent.mm:id/ekf').text print('抓取到小帅b朋友圈数据: %s' % moment_text) print('抓取到小帅b发布时间: %s月%s' % (month_text, day_text))
Example #3
Source File: test_utils.py From sos-notebook with BSD 3-Clause "New" or "Revised" License | 6 votes |
def wait_for_selector(browser, selector, timeout=10, visible=False, single=False): wait = WebDriverWait(browser, timeout) if single: if visible: conditional = EC.visibility_of_element_located else: conditional = EC.presence_of_element_located else: if visible: conditional = EC.visibility_of_all_elements_located else: conditional = EC.presence_of_all_elements_located return wait.until(conditional((By.CSS_SELECTOR, selector)))
Example #4
Source File: screenshots.py From incubator-superset with Apache License 2.0 | 5 votes |
def get_screenshot( self, url: str, element_name: str, user: "User", retries: int = SELENIUM_RETRIES ) -> Optional[bytes]: driver = self.auth(user) driver.set_window_size(*self._window) driver.get(url) img: Optional[bytes] = None logger.debug("Sleeping for %i seconds", SELENIUM_HEADSTART) time.sleep(SELENIUM_HEADSTART) try: logger.debug("Wait for the presence of %s", element_name) element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, element_name)) ) logger.debug("Wait for .loading to be done") WebDriverWait(driver, 60).until_not( EC.presence_of_all_elements_located((By.CLASS_NAME, "loading")) ) logger.info("Taking a PNG screenshot") img = element.screenshot_as_png except TimeoutException: logger.error("Selenium timed out") except WebDriverException as ex: logger.error(ex) # Some webdrivers do not support screenshots for elements. # In such cases, take a screenshot of the entire page. img = driver.screenshot() # pylint: disable=no-member finally: self.destroy(driver, retries) return img
Example #5
Source File: scanner.py From traxss with MIT License | 5 votes |
def run_on_url(self): print(blue('[*] Running XSS Scan [*]')) options = webdriver.ChromeOptions() options.add_argument('--headless') self.driver = webdriver.Chrome(chrome_options=options) query_window, html_window = self.setup_windows() webelement_list = WebDriverWait(self.driver, 10).until(expected_conditions.presence_of_all_elements_located((By.XPATH, "//input | //textarea | //button"))) for payload in self.payloads: self.driver.switch_to.window(query_window) self.query_scanner(payload) if self.html_scan: self.driver.switch_to.window(html_window) self.html_scanner(payload, webelement_list, self.base_url) self.final_report()
Example #6
Source File: test_assignment_list.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _wait_for_modal(browser): _wait(browser).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".modal-dialog")))
Example #7
Source File: test_create_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_locked_cell(browser, port): _load_notebook(browser, port) _activate_toolbar(browser) # does the nbgrader metadata exist? assert _get_metadata(browser) is None # make it locked _select_locked(browser) assert not _get_metadata(browser)['solution'] assert not _get_metadata(browser)['grade'] assert _get_metadata(browser)['locked'] # wait for the id and lock button to appear WebDriverWait(browser, 30).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-id"))) WebDriverWait(browser, 30).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".lock-button"))) # set the id assert _get_metadata(browser)['grade_id'].startswith("cell-") _set_id(browser) assert "foo" == _get_metadata(browser)['grade_id'] # make sure the metadata is valid _save_and_validate(browser) # make it nothing _select_none(browser) assert not _get_metadata(browser) _save_and_validate(browser)
Example #8
Source File: test_create_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_tests_cell(browser, port): _load_notebook(browser, port) _activate_toolbar(browser) # does the nbgrader metadata exist? assert _get_metadata(browser) is None # make it autograder tests _select_tests(browser) assert not _get_metadata(browser)['solution'] assert _get_metadata(browser)['grade'] assert _get_metadata(browser)['locked'] # wait for the points and id fields to appear _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-points"))) _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-id"))) WebDriverWait(browser, 30).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".lock-button"))) # set the points _set_points(browser) assert 2 == _get_metadata(browser)['points'] # set the id assert _get_metadata(browser)['grade_id'].startswith("cell-") _set_id(browser) assert "foo" == _get_metadata(browser)['grade_id'] # make sure the metadata is valid _save_and_validate(browser) # make it nothing _select_none(browser) assert not _get_metadata(browser) _save_and_validate(browser)
Example #9
Source File: test_create_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_task_cell(browser, port): _load_notebook(browser, port, name='task') _activate_toolbar(browser) # does the nbgrader metadata exist? assert _get_metadata(browser) is None # make it manually graded _select_task(browser) assert _get_metadata(browser)['task'] assert not _get_metadata(browser)['solution'] assert not _get_metadata(browser)['grade'] assert _get_metadata(browser)['locked'] # wait for the points and id fields to appear _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-points-input"))) _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-id-input"))) # set the points _set_points(browser) assert 2 == _get_metadata(browser)['points'] # set the id assert _get_metadata(browser)['grade_id'].startswith("cell-") _set_id(browser) assert "foo" == _get_metadata(browser)['grade_id'] # make sure the metadata is valid _save_and_validate(browser) # make it nothing _select_none(browser) assert not _get_metadata(browser) _save_and_validate(browser)
Example #10
Source File: test_create_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_manual_cell(browser, port): _load_notebook(browser, port) _activate_toolbar(browser) # does the nbgrader metadata exist? assert _get_metadata(browser) is None # make it manually graded _select_manual(browser) assert _get_metadata(browser)['solution'] assert _get_metadata(browser)['grade'] assert not _get_metadata(browser)['locked'] # wait for the points and id fields to appear _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-points-input"))) _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-id-input"))) # set the points _set_points(browser) assert 2 == _get_metadata(browser)['points'] # set the id assert _get_metadata(browser)['grade_id'].startswith("cell-") _set_id(browser) assert "foo" == _get_metadata(browser)['grade_id'] # make sure the metadata is valid _save_and_validate(browser) # make it nothing _select_none(browser) assert not _get_metadata(browser) _save_and_validate(browser)
Example #11
Source File: test_create_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _wait_for_modal(browser): _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".modal-dialog")))
Example #12
Source File: test_create_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _activate_toolbar(browser, name="Create%20Assignment"): def celltoolbar_exists(browser): return browser.execute_script( """ return typeof $ !== "undefined" && $ !== undefined && $("#view_menu #menu-cell-toolbar").find("[data-name=\'{}\']").length == 1; """.format(name)) # wait for the view menu to appear _wait(browser).until(celltoolbar_exists) # activate the Create Assignment toolbar browser.execute_script( "$('#view_menu #menu-cell-toolbar').find('[data-name=\"{}\"]').find('a').click();".format(name) ) # make sure the toolbar appeared if name == "Create%20Assignment": _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".celltoolbar select"))) elif name == "Edit%20Metadata": _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".celltoolbar button")))
Example #13
Source File: test_validate_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _wait_for_modal(browser): _wait(browser).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".modal-dialog")))
Example #14
Source File: harvest_utils.py From DLink_Harvester with GNU General Public License v3.0 | 5 votes |
def Elems(self,css:str) -> [WebElement]: return self._wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,css)))
Example #15
Source File: wait_operations.py From warriorframework with Apache License 2.0 | 5 votes |
def wait_until_presence_of_all_elements_located(self, browser_instance, locator_type, locator, timeout=5): try: WebDriverWait(browser_instance, int(timeout)).until(EC.presence_of_all_elements_located((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("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
Example #16
Source File: tv.py From Kairos with GNU General Public License v3.0 | 5 votes |
def find_elements(browser, locator, locator_strategy=By.CSS_SELECTOR, except_on_timeout=True, visible=False, delay=CHECK_IF_EXISTS_TIMEOUT): if except_on_timeout: if visible: elements = WebDriverWait(browser, delay).until( ec.visibility_of_all_elements_located((locator_strategy, locator))) else: elements = WebDriverWait(browser, delay).until( ec.presence_of_all_elements_located((locator_strategy, locator))) return elements else: try: if visible: elements = WebDriverWait(browser, delay).until( ec.visibility_of_all_elements_located((locator_strategy, locator))) else: elements = WebDriverWait(browser, delay).until( ec.presence_of_all_elements_located((locator_strategy, locator))) return elements except TimeoutException as e: log.debug(e) log.debug("Check your {} locator: {}".format(locator_strategy, locator)) # print the session_id and url in case the element is not found # noinspection PyProtectedMember log.debug("In case you want to reuse session, the session_id and _url for current browser session are: {},{}".format(browser.session_id, browser.command_executor._url)) return None
Example #17
Source File: common.py From pixelated-user-agent with GNU Affero General Public License v3.0 | 4 votes |
def _wait_until_elements_are_visible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S): wait = WebDriverWait(context.browser, timeout) wait.until(EC.presence_of_all_elements_located(locator_tuple)) return context.browser.find_elements(locator_tuple[0], locator_tuple[1])
Example #18
Source File: test_utils.py From sos-notebook with BSD 3-Clause "New" or "Revised" License | 4 votes |
def _wait_for(driver, locator_type, locator, timeout=10, visible=False, single=False): """Waits `timeout` seconds for the specified condition to be met. Condition is met if any matching element is found. Returns located element(s) when found. Args: driver: Selenium web driver instance locator_type: type of locator (e.g. By.CSS_SELECTOR or By.TAG_NAME) locator: name of tag, class, etc. to wait for timeout: how long to wait for presence/visibility of element visible: if True, require that element is not only present, but visible single: if True, return a single element, otherwise return a list of matching elements """ wait = WebDriverWait(driver, timeout) if single: if visible: conditional = EC.visibility_of_element_located else: conditional = EC.presence_of_element_located else: if visible: conditional = EC.visibility_of_all_elements_located else: conditional = EC.presence_of_all_elements_located return wait.until(conditional((locator_type, locator)))
Example #19
Source File: __init__.py From AutoXue with MIT License | 4 votes |
def view_score(self): self.safe_click(rules['score_entry']) titles = ["登录", "阅读文章", "视听学习", "文章学习时长", "视听学习时长", "每日答题", "每周答题", "专项答题", "挑战答题", "订阅", "收藏", "分享", "发表观点"] score_list = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, rules['score_list']))) for t, score in zip(titles, score_list): s = score.get_attribute("name") self.score[t] = tuple([int(x) for x in re.findall(r'\d+', s)]) # print(self.score) for i in self.score: logger.debug(f'{i}, {self.score[i]}') self.safe_back('score -> home')
Example #20
Source File: xhs_app.py From xhs_simple_crawler with MIT License | 4 votes |
def scroll(self): while True: # 当前页面显示的所有状态 items = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.xingin.xhs:id/a1z'))) # 遍历每条状态 for item in items: try: # 昵称 nickname = item.find_element_by_id('com.xingin.xhs:id/bhs').get_attribute('text') # 正文 content = item.find_element_by_id('com.xingin.xhs:id/anl').get_attribute('text') # 日期 date = item.find_element_by_id('com.xingin.xhs:id/ask').get_attribute('text') # 处理日期 date = self.processor.date(date) print(nickname, content, date) data = { 'nickname': nickname, 'content': content, 'date': date, } # 插入MongoDB self.collection.update({'nickname': nickname, 'content': content}, {'$set': data}, True) sleep(SCROLL_SLEEP_TIME) except NoSuchElementException: pass # 上滑 self.driver.swipe(FLICK_START_X, FLICK_START_Y + FLICK_DISTANCE, FLICK_START_X, FLICK_START_Y)
Example #21
Source File: ZhiHuUtil.py From Machine-Learning-Study-Notes with Apache License 2.0 | 4 votes |
def getFriendList(broswer, userID, currentLevel=1): try: if currentLevel > totalLevel: return if userID == 'no data': raise Exception() nameTemp = userID.split('/')[2] if not nameTemp in alreadyParse: alreadyParse.add(nameTemp) else: return print('......*' * currentLevel ,'正在解析用户:', nameTemp, '知乎首页:https://www.zhihu.com%s' % (userID), sep=' ') friendSet = set() broswer.get('https://www.zhihu.com%s' % (userID)) WebDriverWait(broswer, 10).until( expected_conditions.presence_of_all_elements_located((By.CSS_SELECTOR, '.UserLink-link'))) elt = WebDriverWait(broswer, 10).until( expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '.Avatar.Avatar--large.UserAvatar-inner'))) res = re.match('^(https://.*)[0-9]x$', elt.get_attribute('srcset')) if res: if not nameTemp in alreadyDownload: alreadyDownload.add(nameTemp) url = res.group(1) writeToFile(url, '%s.%s' % (nameTemp ,url.split('.')[-1])) print('......*' * currentLevel, '已经下载', nameTemp, '的用户头像', '知乎首页:https://www.zhihu.com%s' % (userID), sep=' ') except: print('......*' * currentLevel, 'getFriendList异常') else: print('......*' * currentLevel, '正在获取用户', nameTemp, '的关注列表...', sep=' ') bsObj = BeautifulSoup(broswer.page_source, 'html.parser') elts = bsObj.findAll('a', {'class':'UserLink-link'}) for elt in elts: img = elt.find('img') if img: friendSet.add(elt) print('......*' * currentLevel, 'https://www.zhihu.com%s' % (elt.attrs.get('href', 'no data'))) elts = bsObj.findAll('button', {'class':'Button PaginationButton Button--plain'}) if len(elts) != 0: count = elts[len(elts) - 1].get_text() for i in range(2, int(count) + 1): getAllFriendList(broswer, userID, i, friendSet, currentLevel) print('......*' * currentLevel, '用户', nameTemp, '的关注列表获取完毕', sep=' ') for elt in friendSet: href = elt.attrs.get('href', 'no data') if currentLevel == totalLevel: img = elt.find('img') if img: res = re.match('^(https://.*)[0-9]x$', img.attrs.get('srcset', 'no data')) if res: if not href.split('/')[2] in alreadyDownload: alreadyDownload.add(href.split('/')[2]) url = res.group(1).replace('_xl', '_xll') writeToFile(url, '%s.%s' % (href.split('/')[2] ,url.split('.')[-1])) print('......*' * (currentLevel + 1), '已经下载用户',nameTemp, '的关注用户', href.split('/')[2], '的头像', sep=' ') getFriendList(broswer, '%s/%s' % (href, userID.split('/')[3]), currentLevel + 1)
Example #22
Source File: test_create_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_tests_to_solution_cell(browser, port): _load_notebook(browser, port) _activate_toolbar(browser) # does the nbgrader metadata exist? assert _get_metadata(browser) is None # make it autograder tests _select_tests(browser) assert not _get_metadata(browser)['solution'] assert _get_metadata(browser)['grade'] assert _get_metadata(browser)['locked'] # wait for the points and id fields to appear _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-points"))) _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-id"))) WebDriverWait(browser, 30).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".lock-button"))) # set the points _set_points(browser) assert 2 == _get_metadata(browser)['points'] # set the id assert _get_metadata(browser)['grade_id'].startswith("cell-") _set_id(browser) assert "foo" == _get_metadata(browser)['grade_id'] # make sure the metadata is valid _save_and_validate(browser) # make it a solution cell and make sure the points are gone _select_solution(browser) assert _get_metadata(browser)['solution'] assert not _get_metadata(browser)['grade'] assert not _get_metadata(browser)['locked'] assert 'points' not in _get_metadata(browser) _save_and_validate(browser) # make it nothing _select_none(browser) assert not _get_metadata(browser) _save_and_validate(browser)
Example #23
Source File: ZhiHuUtil.py From Machine-Learning-Study-Notes with Apache License 2.0 | 4 votes |
def getAllFriendList(broswer, userID, pageNum, friendSet, currentLevel): try: broswer.get('https://www.zhihu.com%s?page=%s' % (userID, pageNum)) WebDriverWait(broswer, 10).until( expected_conditions.presence_of_all_elements_located((By.CSS_SELECTOR, '.UserLink-link'))) except: print('getAllFriendList异常') else: bsObj = BeautifulSoup(broswer.page_source, 'html.parser') elts = bsObj.findAll('a', {'class':'UserLink-link'}) for elt in elts: img = elt.find('img') if img: friendSet.add(elt) print('......*' * currentLevel, 'https://www.zhihu.com%s' % (elt.attrs.get('href', 'no data')))
Example #24
Source File: test_create_assignment.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_invalid_nbgrader_cell_type(browser, port): _load_notebook(browser, port) _activate_toolbar(browser) # make it a solution cell _select_solution(browser) assert _get_metadata(browser)['solution'] assert not _get_metadata(browser)['grade'] assert not _get_metadata(browser)['locked'] # wait for the id field to appear _wait(browser).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".nbgrader-id"))) # set the id assert _get_metadata(browser)['grade_id'].startswith("cell-") _set_id(browser) assert _get_metadata(browser)['grade_id'] == "foo" # make sure the metadata is valid _save_and_validate(browser) # change the cell to a markdown cell element = browser.find_element_by_tag_name("body") element.send_keys(Keys.ESCAPE) element.send_keys("m") # make sure the toolbar appeared def find_toolbar(browser): try: browser.find_elements_by_css_selector(".celltoolbar select")[0] except IndexError: return False return True _wait(browser).until(find_toolbar) # check that then nbgrader metadata is consistent assert not _get_metadata(browser)['solution'] assert not _get_metadata(browser)['grade'] assert not _get_metadata(browser)['locked'] assert _get_metadata(browser)['grade_id'] == "foo" ################################################################################ ####### DO NOT ADD TESTS BELOW THIS LINE ####################################### ################################################################################
Example #25
Source File: amazon_bot.py From youtube_tutorials with GNU General Public License v3.0 | 4 votes |
def search_items(self): """Searches through the list of items obtained from spreadsheet and obtains name, price, and URL information for each item.""" urls = [] prices = [] names = [] for item in self.items: print(f"Searching for {item}...") self.driver.get(self.amazon_url) #select = Select(self.driver.find_element_by_id("searchDropdownDescription")) #select.select_by_visible_text('All Departments') search_input = self.driver.find_element_by_id("twotabsearchtextbox") search_input.send_keys(item) time.sleep(2) #wait = WebDriverWait(self.driver, self.explicit_wait) #wait.until(EC.presence_of_all_elements_located((By.ID, "twotabsearchtextbox"))) search_button = self.driver.find_element_by_xpath('//*[@id="nav-search"]/form/div[2]/div/input') search_button.click() time.sleep(2) t = self.driver.find_element_by_id("result_0") asin = t.get_attribute("data-asin") url = "https://www.amazon.ca/dp/" + asin price = self.get_product_price(url) name = self.get_product_name(url) prices.append(price) urls.append(url) names.append(name) print(name) print(price) print(url) time.sleep(2) return prices, urls, names