def get_max_page_num(self): max_num = WebDriverWait(self.driver, 20, 0.1).until( lambda d: d.find_element_by_class_name(self.TOTAL_NUM)) size = max_num.get_attribute("innerText") if not size: size = max_num.text return int(size)
def __init__(self): name = "" pw = "" while((name == "") | (pw == "")): name = input("Loginname: ") print("Login :"******"Password: "******"PW :", pw) dcap = dict(DesiredCapabilities.PHANTOMJS) dcap["phantomjs.page.settings.userAgent"] = ( "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 (KHTML, like Gecko) Chrome/15.0.87") self.driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true']) #goto isis page because direct access is not permitted self.driver.get("https://isis.tu-berlin.de/") #wait until page is up wait = WebDriverWait(self.driver, 10) wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.content"))) #click shibbolethbutton to go to login page self.driver.find_element_by_id('shibbolethbutton').click() # Fill the login form and submit it self.driver.find_element_by_name('j_username').send_keys(name) self.driver.find_element_by_id('password').send_keys(pw) self.driver.find_element_by_name('Submit').submit()
def test_onclick_popup_modal_and_presents_articles_titles(self): # self.create_database_record() # Visitor goes to home page and clicks on airplane word. self.browser.get(self.live_server_url) self.get_word('airplane').click() # Visitor waits until modal windows pop up. block = WebDriverWait(self.browser, 10) modal = block.until(expected_conditions.visibility_of_element_located((By.ID, 'word-details-modal'))) # Visitor gets articles titles. self.check_for_title_in_article_table('airplane is faster than ship.') self.check_for_title_in_article_table('airplane crashed yesterday.') self.check_title_does_not_exist_in_article_table('rocket took off today morning.') # Visitor closes modal window, refreshes browser and clicks on rocket word. self.browser.find_element(By.XPATH, '//button[@class="btn btn-default"]').click() self.browser.refresh() self.browser.find_element_by_link_text('rocket').click() # Visitor waits until modal windows pop up. block = WebDriverWait(self.browser, 10) modal = block.until(expected_conditions.visibility_of_element_located((By.ID, 'word-details-modal'))) # Visitor gets articles titles. self.check_title_does_not_exist_in_article_table('airplane is faster than ship.') self.check_title_does_not_exist_in_article_table('airplane crashed yesterday.') self.check_for_title_in_article_table('rocket took off today morning.')
def test_see_group(self): """ I should be able to log in as the first user. I should be able to see one group. When clicking on that group, I should be able to see 1 file for thar group. """ welcome_url = '%s%s' % (self.live_server_url, '/welcome') self.driver.get(welcome_url) css_selector_email="input#login_user_email" css_selector_password="******" css_selector_submit="button#auth_submit" css_selector_loginname="a#login-name" def try_to_login(user): self.bycss(css_selector_email).send_keys(user.email) self.bycss(css_selector_password).send_keys(user.password) self.bycss(css_selector_submit).click() element = WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_css_selector(css_selector_loginname)) self.assertEqual(element.text, "%s %s" % (user.firstname, user.lastname), "user name didn't match") try_to_login(User.objects.all().order_by("id")[0]) #try to login as first user. self.driver.get(self.live_server_url) a = self.bycss(".jstree-leaf[rel=ensemble][id_item='1']") ensemble_name=Ensemble.objects.get(pk=1).name self.assertEqual(a.text, ensemble_name, "ensemble name didn't match") #now try and click the link: a2 = a.find_element_by_tag_name("a") a2.click() file_elt = WebDriverWait(self.driver, 3).until(lambda x: x.find_element_by_css_selector(".filesview_ftitle>a")) self.assertEqual(file_elt.text, Source.objects.get(pk=1).title, "source title didn't match") file_elt.click() #check for instance that NB.pers.store.o.file[1].title is the same title pass
class Application(object): def __init__(self, driver): self.wd = driver self.base_url = "http://localhost/" self.session = SessionHelper(self) self.movie = MovieHelper(self) # self.wait = WaitHelper(self) self.wait = WebDriverWait(self.wd, 30) def wait_element_off(self, element): self.wait.until(staleness_of(element)) def wait_element_on(self): wd = self.wd element = self.wait.until(presence_of_element_located((By.XPATH, "//div[@class='movie_box']"))) if element != wd.find_element_by_xpath("//div[@class='movie_box']"): pass #self.session.logout() def open_add_movie_page(self): wd = self.wd wd.get(self.base_url + "/php4dvd/?go=add") def open_home_page(self): wd = self.wd wd.get(self.base_url + "/php4dvd/") def destroy(self): self.wd.quit()
def test_check_all_admin_pages(self): wait = WebDriverWait(self.driver, 5) self.driver.get(SIGN_IN_PAGE) self.driver.find_element_by_name("username").send_keys(LOGIN) self.driver.find_element_by_name("password").send_keys(PASSWORD) self.driver.find_element_by_name("login").click() first_menu_elements = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#app->a"))) first_menu_links = [x.get_attribute("href") for x in first_menu_elements] for link in first_menu_links: self.driver.get(link) first_menu_item_header = self.driver.find_elements_by_css_selector("#content>h1") self.assertTrue(first_menu_item_header) second_menu_elements = self.driver.find_elements_by_css_selector(".docs a") second_menu_links = [x.get_attribute("href") for x in second_menu_elements] if second_menu_elements: for link in second_menu_links: self.driver.get(link) second_menu_item_header = self.driver.find_elements_by_css_selector("#content>h1") self.assertTrue(second_menu_item_header)
def get_page_num_from_browser(self): current_num = WebDriverWait(self.driver, 20, 0.1).until( lambda d: d.find_element_by_xpath(self.CURRENT_NUM)) size = current_num.get_attribute("innerText") if not size: size = current_num.text return int(size)
def delete_user(self, name=None): #local locators _locUser = (By.XPATH, "//h3[attribute::title='"+name+"']/preceding-sibling::div[attribute::class='controls']") _locDelButton = (By.CSS_SELECTOR, "button") #[H] move to the user page self.__move_to_user_page() #[M] find the user to leave out of the user list isGivenUserFound = False try: user = WebDriverWait(self.selenium,self._TimeOut). \ until(lambda s: self.find_element(*_locUser)) #take a delete action on the element delButton = WebDriverWait(self.selenium,self._TimeOut). \ until(lambda s: user.find_element(*_locDelButton)) isGivenUserFound = True except Exceptions.TimeoutException: print "let us move on..\n" if isGivenUserFound == True: #click the button delButton.click() #return the outcome in boolean return isGivenUserFound
def get(self, url): super(WebDriverMixin, self).get('about:blank') full_url = urljoin(self._base_url, url) self.execute_script( """ window.name = "{}" + window.name; window.location.replace("{}"); """.format(DEFER_LABEL, full_url) ) wait = WebDriverWait(self, 10) wait.until_not(self._location_equals, 'about:blank') if not self.ignore_synchronization: test_result = self._test_for_angular() angular_on_page = test_result[0] if not angular_on_page: message = test_result[1] raise AngularNotFoundException( 'Angular could not be found on page: {}:' ' {}'.format(full_url, message) ) # TODO: inject scripts here # return self.execute_script( # 'angular.resumeBootstrap(arguments[0]);' # ) self.execute_script('angular.resumeBootstrap();')
def _get_keyspaces(self): """Get list of all present keyspaces.""" wait = WebDriverWait(self.driver, 10) wait.until(expected_conditions.visibility_of_element_located((By.TAG_NAME, "vt-dashboard"))) dashboard_content = self.driver.find_element_by_tag_name("vt-dashboard") toolbars = dashboard_content.find_elements_by_class_name("vt-card-toolbar") return [t.find_element_by_class_name("vt-title").text for t in toolbars]
class appledailyAndroidTests(unittest.TestCase): def setUp(self): desired_caps = {} desired_caps['browserName'] = '' desired_caps['device'] = 'Android' desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.2。2' desired_caps['deviceName'] = 'S5' desired_caps['app'] = PATH( u'D:/测试APP/com.nextmedia.apk' ) desired_caps['appPackage'] = 'com.nextmedia' desired_caps['appActivity'] = 'com.nextmedia.apple.hk.Loading' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) self.wait = WebDriverWait(self.driver, 60) def tearDown(self): self.driver.quit() def gettextviewtext(self): textview=self.driver.find_elements_by_class_name('android.widget.TextView') i=0 list = [] while i<len(textview): list.append(textview[i].text) i=i+1 return list def entermainpage(self): self.driver.find_element_by_id('com.nextmedia:id/tutorial_show').click() self.driver.find_element_by_name(u'略過').click() self.wait.until(EC.element_to_be_clickable((By.ID,'com.nextmedia:id/ranking_button')))
def _get_keyspace_shards(self): wait = WebDriverWait(self.driver, 10) wait.until(expected_conditions.visibility_of_element_located( (By.TAG_NAME, 'vt-keyspace-view'))) keyspace_content = self.driver.find_element_by_tag_name('vt-keyspace-view') return [sh.text for sh in keyspace_content.find_elements_by_class_name('vt-serving-shard')]
def wait_until_element_not_visible(webdriver, locator_lambda_expression, timeout=WTF_TIMEOUT_MANAGER.NORMAL, sleep=0.5): """ Wait for a WebElement to disappear. Args: webdriver (Webdriver) - Selenium Webdriver locator (lambda) - Locator lambda expression. Kwargs: timeout (number) - timeout period sleep (number) - sleep period between intervals. """ # Wait for loading progress indicator to go away. try: stoptime = datetime.now() + timedelta(seconds=timeout) while datetime.now() < stoptime: element = WebDriverWait(webdriver, WTF_TIMEOUT_MANAGER.BRIEF).until( locator_lambda_expression) if element.is_displayed(): time.sleep(sleep) else: break except TimeoutException: pass
def add_service(self): count = self.items_in_cart() Find(by=By.CSS_SELECTOR, value='.add-to-cart', context=self).click() if not self.item_is_sold_out(): wait = WebDriverWait(self._driver, 10) wait.until(lambda x: count < self.items_in_cart())
def banner_save(self): self.wait_banner_image() element = WebDriverWait(self.driver, 30, 3).until( lambda el: el.find_element_by_css_selector(self.BANNER_SAVE) ) element.click()
def _attempt_download(self, query, offset): real_offset = self._calculate_real_offset(offset) real_offset = int(real_offset) dictionary = {AbsWebsiteCommonApi._QUERY_MASK: query, AbsWebsiteCommonApi._OFFSET_MASK: str(real_offset)} url = self._multiple_replace(dictionary, self.base_url) page_source = None for i in range(0, AbsWebsiteCommonApi._DOWNLOAD_TRY_NUMBER): time.sleep(AbsWebsiteCommonApi._CRAWL_DELAY) web_page = None try: web_page = webdriver.PhantomJS() web_page.get(url) wait = WebDriverWait(web_page, AbsWebsiteCommonApi._PAGE_LOAD_TIMEOUT) wait.until(self.test_page_loaded) except Exception as exception: print(str(exception)) if web_page is not None: web_page.close() page_source = None continue page_source = web_page.execute_script(AbsWebsiteCommonApi._JAVASCRIPT_GET_PAGE_SOURCE_CODE) web_page.close() self.inc_download() break if page_source is None: print("ERROR - Internet connection failure") os.kill(os.getpid(), signal.SIGUSR1) return page_source
def wait_element_load_end(self, element): wait = WebDriverWait(self.driver, waiting_time) try: wait.until(ec.presence_of_element_located(*element)) wait.until(ec.element_to_be_clickable(*element)) except: time.sleep(1)
def finds( identifier, context=None, timeout=-1, condition=None): """ @return: Returns the web element found by identifier @rtype: selenium.webdriver.remote.webelement.WebElement """ if timeout == -1: timeout = default_timeout if isinstance(identifier, WebElement): return identifier if context is None: context = driver locator = get_identifier(identifier) if condition is None: condition = count_non_zero_and_visible(locator) else: condition = condition(locator) wdw = WebDriverWait(driver, timeout) try: elems = wdw.until(condition) return elems if isinstance(elems, list) else [] except TimeoutException: return []
def _get_keyspace_element(self, keyspace_name): """Get a specific keyspace element given a keyspace name.""" element_id = '%s-card' % keyspace_name wait = WebDriverWait(self.driver, self.WEBDRIVER_TIMEOUT_S) wait.until(expected_conditions.visibility_of_element_located( (By.ID, element_id))) return self.driver.find_element_by_id(element_id)
def _login(self): fb_login_button = self._css('.fb-login-button') self._assert_visible(fb_login_button) time.sleep(0.1) fb_login_button.click() wait = WebDriverWait(self.driver, 2) wait.until(window_opened) self.driver.switch_to_window(self.driver.window_handles[1]) wait.until(lambda d: d.find_element_by_name('email')) email_input = self.driver.find_element_by_name('email') pass_input = self.driver.find_element_by_name('pass') email_input.send_keys('*****@*****.**') pass_input.send_keys('flowtestpass') pass_input.submit() self.driver.switch_to_window(self.driver.window_handles[0]) wait = WebDriverWait(self.driver, 10) wait.until(EC.title_contains('Welcome')) wait.until(lambda d: d.find_element_by_class_name("transcript-text"))
def test_select_suggest(self): self.driver.execute_script("mobile: OnScreenKeyboard.Disable") pivots = self.driver.find_elements_by_class_name("Windows.UI.Xaml.Controls.Primitives.PivotHeaderItem") pivots[1].click() waiter = WebDriverWait(self.driver, timeout=5) autosuggestion_box = waiter.until(EC.presence_of_element_located((By.ID, 'MySuggestBox'))) autosuggestion_input = autosuggestion_box.find_element_by_class_name('Windows.UI.Xaml.Controls.TextBox') autosuggestion_input.send_keys('A') suggestions_list = waiter.until(EC.presence_of_element_located((By.XNAME, 'SuggestionsList'))) suggestions = suggestions_list.find_elements_by_class_name('Windows.UI.Xaml.Controls.TextBlock') expected_text = 'A2' for suggest in suggestions: if suggest.text == expected_text: # When AutoSuggest is focused it moves whole view up # but it is not reflected coordinates that we get from driver, # instead of suggest.click() we will have to use Select pattern point = self.driver.execute_script('automation: GetClickablePoint', suggest) x, y = [float(v) for v in point.split(',')] ActionChains(self.driver).move_by_offset(x, y).click().perform() break assert expected_text == autosuggestion_input.text
def test_check_countries(self): wait = WebDriverWait(self.driver, 5) self.sign_in() self.driver.get(COUNTRIES_PAGE) current_countries = [] not_null_zones_country_links = [] country_rows = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".row"))) for row in country_rows: country = row.find_element_by_css_selector("td:nth-child(5) a") current_countries.append(country.text) zones = row.find_element_by_css_selector("td:nth-child(6)") if int(zones.text) > 0: not_null_zones_country_links.append(country.get_attribute("href")) expected_countries = sorted(current_countries) self.assertListEqual(current_countries, expected_countries) for link in not_null_zones_country_links: self.driver.get(link) current_country_zones = [x.text for x in wait.until(EC.presence_of_all_elements_located( (By.XPATH, "(.//*[@id=\"table-zones\"]//td[3])[position() < last()]") ))] expected_country_zones = sorted(current_country_zones) self.assertListEqual(current_country_zones, expected_country_zones)
def click_first_nearby_stop(self, place_name): self.click_first_search_place(place_name) wait = WebDriverWait(self.driver, 15) first_nearby_stop = wait.until( lambda driver: self.driver.find_element_by_xpath("//android.widget.TextView[@index='0' and @resource-id='nexti.android.bustaipei:id/text_stopname']") ) first_nearby_stop.click()
def is_business_node_checked(self, business_node_number): checkbox = WebDriverWait(self.driver, 30, 0.5).until( lambda d: d.find_elements_by_css_selector(self.BUSINESS_NODES_CHECKBOXES)[business_node_number + 1] ) if checkbox.is_selected(): return True return False
def test_sort_routes(self): wait = WebDriverWait(self.driver, 15) self.click_first_nearby_stop('Taipei Station') wait.until( lambda driver: self.driver.find_element_by_id('nexti.android.bustaipei:id/menu_passby_sort') ).click() sleep(0.5) estimates = self.driver.find_elements_by_id('nexti.android.bustaipei:id/text_estimate') current_estimate = 0 is_depart = False serv_over = False for estimate in estimates: if is_depart: self.assertEqual(estimate.text, 'Depart') elif serv_over: self.assertEqual(estimate.text, 'Serv Over') if estimate.text == 'Depart': is_depart = True continue elif estimate.text == 'Serv Over': serv_over = True continue elif estimate.text == 'Approach': continue match = re.search('(\d{0,2}) min', estimate.text) next_estimate = int(match.group(1)) self.assertLessEqual(current_estimate, next_estimate) current_estimate = next_estimate
def test_compare_products_removal_alert(self): # get the search textbox search_field = self.driver.find_element_by_name("q") search_field.clear() # enter search keyword and submit search_field.send_keys("phones") search_field.submit() # click the Add to compare link self.driver.\ find_element_by_link_text("Add to Compare").click() # click on Remove this item link, this will display # an alert to the user self.driver.find_element_by_link_text("Clear All").click() # switch to the alert #alert = self.driver.switch_to_alert() alert = WebDriverWait(self.driver,10).until(expected_conditions.alert_is_present()) # get the text from alert alert_text = alert.text # check alert text self.assertEqual("Are you sure you would like to remove all products from your comparison?", alert_text) # click on Ok button alert.accept()
def is_checked(self, compared_to_average): checkbox = WebDriverWait(self.driver, 30, 0.5).until( lambda d: d.find_elements_by_css_selector(self.COMPARED_TO_AVERAGE)[compared_to_average] ) if checkbox.is_selected(): return True return False
def is_element_present(self, locator, timeout=3): try: wait = WebDriverWait(self.driver, timeout) wait.until(expected_conditions.presence_of_element_located(locator)) except WebDriverException: return False return True
def publish(link): driver.get(link) find_click(".org-module-link[href*=members]") type(".auto-search-input", username) l = lambda _driver: _driver.find_element_by_css_selector("a[href*=publicize]") elm = WebDriverWait(driver, 25).until(l) elm.click()
def setUp(self): super().setUp() wait = WebDriverWait(self.driver, 15) button = wait.until( lambda driver: self.driver.find_element_by_android_uiautomator('new UiSelector().text("Nearby")') ) button.click()
def wait_for_click(self, locator, timeout=10): ele = WebDriverWait(self.driver, timeout).until( expected_conditions.element_to_be_clickable(locator)) return ele
def choose_ticket_2(self): # for type 2, i.e., piao.damai.cn self.time_start = time() print("###开始进行日期及票价选择###") while self.driver.title.find('订单结算页') == -1: # 如果跳转到了确认界面就算这步成功了,否则继续执行此步 self.num += 1 # 记录抢票轮数 if self.date != 0: # 如果要选择日期 datepicker = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located( (By.CLASS_NAME, "month"))) datelist = datepicker.find_elements_by_tag_name("span") # 找出所有日期 # print(len(datelist)) validlist = [] for i in range(len(datelist)): # 筛选出所有可选择日期 j = datelist[i] k = j.get_attribute('class') if k == 'itm z-show itm-undefined z-sel' \ or k == 'itm z-show itm-undefined' \ or k == 'itm itm-end z-show itm-undefined': validlist.append(j) # print(len(validlist)) validlist[self.date - 1].click() session = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located( (By.ID, "performList"))) # session = self.driver.find_element_by_id('performList') session_list = session.find_elements_by_tag_name('li') print('可选场次数量为:{}'.format(len(session_list))) for i in self.session: # 根据优先级选择一个可行场次,目前暂时没有找到有不可行日期的案例 j = session_list[i - 1] k = j.get_attribute('class').strip() if k == 'itm' or k == 'itm j_more': # 未选中 j.find_element_by_tag_name('a').click() break elif k == 'itm itm-sel' or k == 'itm j_more itm-sel': # 已选中 break elif k == 'itm itm-oos': # 无法选中 continue sleep(self.intersect_wait_time) price = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located( (By.ID, "priceList"))) # price = self.driver.find_element_by_id('priceList') price_list = price.find_elements_by_tag_name('li') print('可选票档数量为:{}'.format(len(price_list))) for i in self.price: j = price_list[i - 1] k = j.get_attribute('class').strip() if k == 'itm' or k == 'itm j_more': # 未选中 j.find_element_by_tag_name('a').click() break elif k == 'itm itm-sel' or k == 'itm j_more itm-sel': # 已选中 break elif k == 'itm itm-oos': # 无法选中 continue buybutton = None try: buybutton = self.driver.find_element_by_id('btnBuyNow') # 要改成立即预订按钮的id self.status = 3 except: try: buybutton = self.driver.find_element_by_id('btnBuyNow') self.status = 4 except: print('###无法立即购买,尝试自行选座###') try: buybutton = self.driver.find_element_by_id('btnXuanzuo') self.status = 5 print("###请自行选择位置和票价###") break except: print('---尚未开售,刷新等待---') self.status = 2 self.driver.refresh() # 需要先判断是否存在按钮,才能确定是否会出现添加票 if self.ticket_num > 1 and self.status not in [2, 5]: # 自动添加购票数 # add = self.driver.find_element_by_class_name('btn btn-add') add = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located( (By.CLASS_NAME, "btn-add"))) for i in range(self.ticket_num - 1): add.click() buybutton.click()
def wait_until_and_get_elem_by_xpath(self, elem): return WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located((By.XPATH, elem)))
def test_4(app_setup, task4_url): driver = app_setup driver.get(task4_url) wait = WebDriverWait(driver, 10) numberOfGroups = len(driver.find_elements_by_xpath( '/html/body/div/div/h5')) # number of groups in the task numberOfSteps = len( driver.find_elements_by_xpath( '/html/body/div/table/tbody/tr')) # number of steps in instruction numberOfOptions = len( driver.find_elements_by_xpath('/html/body/div/div[1]/input') ) # number of radio buttons to select in group colorsName = [ 'Beluga Brown', 'Mango Orange', 'Verdoro Green', 'Freudian Gilt', 'Pink Kong', 'Duck Egg Blue', 'Anti - Establishment Mint', 'Amberlite Firemist' ] # Posible color names to select instructions = [] # empty list for instructions in the task optDict = { } # empty dictionary for group, color name keys with radio button value currentOut = driver.find_element_by_id('trail') # outcome web element currentOutTxt = currentOut.text # string for compare changes after selected option (Trail...) btnCheck = driver.find_element_by_id('solution') for i in range(2, numberOfSteps + 1): # create list of steps to execute step = driver.find_element_by_xpath( "/html/body/div/table/tbody/tr[{}]/td[2]/code".format(i)).text instructions.append(step) for g in range(1, numberOfGroups + 1): # create dictionary to all radio button groups group = driver.find_elements_by_xpath( '/html/body/div/div[{}]/input'.format(g)) for x in range(1, numberOfOptions + 1): # create 2 keys dictionary for a group. valueName = group[x - 1].get_attribute('value') groupIdx = group[x - 1].get_attribute('name') optDict[groupIdx, colorsName[x - 1]] = valueName # eg. (s0, Beluga Brown) = v01 for select, groupNumber in zip( instructions, range(numberOfGroups)): # loop to execute instruction steps keys = ('s' + str(groupNumber), select) if keys in optDict: value = optDict[ keys] # value for specific radio button in the group driver.find_element_by_xpath( ".//input[@type='radio' and @value='{}']".format(value)).click() wait.until_not( EC.text_to_be_present_in_element( (By.ID, 'trail'), currentOutTxt)) # waiting until text change currentOutTxt = currentOut.text # assigne new text value to compare in next loop step btnCheck.click() wait.until(EC.text_to_be_present_in_element( (By.ID, 'trail'), 'OK')) # wait until possible outcome appear (NOT OK / Ok. Good answer) currentOutTxt = currentOut.text # assigne new text value to assert assert currentOutTxt == 'OK. Good answer'
def __init__(self, driver, wait=2): self.driver = driver self.wait = WebDriverWait(driver, wait) self.logger = logging.getLogger(type(self).__name__)
def __init__(self, driver): self.driver = driver self.wait = WebDriverWait(self.driver, 15) self.base_url = 'https://www.boatsgroup.com/'
def check_order_2(self): if self.status in [3, 4]: print('###开始确认订单###') if self.real_name: # 实名者信息不为空 print('###选择购票人信息###') try: tb = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located( (By.CLASS_NAME, 'from-1'))) tb.find_element_by_tag_name('a').click() # 点击选择购票人按钮 sleep(self.intersect_wait_time) # 此处好像定位不到实名者框,还没有解决 lb_list = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located( (By.XPATH, '/html/body/div[3]/div[3]/div[12]/div/div[2]/div/div[2]/div/table/tbody'))) # 定位弹窗 lb = lb_list.find_elements_by_tag_name('input') for i in range(len(self.real_name)): lb[self.real_name[i] - 1].find_element_by_tag_name('input').click() # 选择第self.real_name个实名者 except Exception as e: print(e) input('halt') WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located( (By.ID, 'orderConfirmSubmit'))).click() # 同意以上协议并提交订单 # self.driver.find_element_by_id('orderConfirmSubmit').click() element = WebDriverWait(self.driver, 10, self.refresh_wait_time).until(EC.title_contains('选择支付方式')) element.find_element_by_xpath('/html/body/div[5]/div/div/div/ul/li[2]/a').click() # 默认选择支付宝 element.find_element_by_xpath('/html/body/div[5]/div/div/form/div[2]/ul/li[1]/label/input').click() element.find_element_by_id('submit2').click() # 确认无误,支付 self.status = 6 print('###成功提交订单,请手动支付###') self.time_end = time()
def _wait_for_element_to_disappear(self, locator, timeout=0): wait = WebDriverWait(self.driver, timeout) wait.until(invisibility_of_element((locator['by'], locator['value'])))
def test_contains_elements(self): driver = self.driver WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//h1[contains(text(),'Online Quiz')]"))) WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//p[contains(text(),'Please answer the Online Quiz below. You will rece')]"))) for i in range(0, 2): WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='00000008_" + str(i) + "']"))) WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[contains(text(),'What is the longest river in the world?')]"))) for i in range(0, 2): WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='0000000a_" + str(i) + "']"))) WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[contains(text(),'Which of the following inventions occured in the 17th century?')]"))) for i in range(0, 4): WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='0000000c_" + str(i) + "']"))) WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[contains(text(),'What name is given to the programs ran by a computer')]"))) for i in range(0, 3): WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='0000000e_" + str(i) + "']"))) WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[contains(text(),'What food group has the highest level')]"))) for i in range(0, 2): WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='00000010_" + str(i) + "']"))) WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[text()='Your Email']"))) WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//input[@type='email']"))) WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//button[@type='submit']")))
def choose_ticket_1(self): # for type 1, i.e., detail.damai.cn self.time_start = time() print("###开始进行日期及票价选择###") while self.driver.title.find('确认订单') == -1: # 如果跳转到了确认界面就算这步成功了,否则继续执行此步 self.num += 1 # 记录抢票轮数 if self.date != 0: # 如果需要选择日期 calendar = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located((By.CLASS_NAME, "functional-calendar"))) datelist = calendar.find_elements_by_css_selector("[class='wh_content_item']") # 找到能选择的日期 datelist = datelist[7:] # 跳过前面7个表示周一~周日的元素 datelist[self.date - 1].click() # 选择对应日期 selects = self.driver.find_elements_by_class_name('perform__order__select') # print('可选区域数量为:{}'.format(len(selects))) for item in selects: if item.find_element_by_class_name('select_left').text == '场次': session = item # print('\t场次定位成功') elif item.find_element_by_class_name('select_left').text == '票档': price = item # print('\t票档定位成功') session_list = session.find_elements_by_class_name('select_right_list_item') print('可选场次数量为:{}'.format(len(session_list))) if len(self.session) == 1: j = session_list[self.session[0] - 1].click() else: for i in self.session: # 根据优先级选择一个可行场次 j = session_list[i - 1] k = self.isClassPresent(j, 'presell', True) if k: # 如果找到了带presell的类 if k.text == '无票': continue elif k.text == '预售': j.click() break else: j.click() break price_list = price.find_elements_by_class_name('select_right_list_item') print('可选票档数量为:{}'.format(len(price_list))) if len(self.price) == 1: j = price_list[self.price[0] - 1].click() else: for i in self.price: j = price_list[i - 1] k = self.isClassPresent(j, 'notticket') if k: # 存在notticket代表存在缺货登记,跳过 continue else: j.click() break try: buybutton = self.driver.find_element_by_class_name('buybtn') buybutton_text = buybutton.text # print(buybutton_text) def add_ticket(): # 设置增加票数 try: for i in range(self.ticket_num - 1): addbtn = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until( EC.presence_of_element_located((By.XPATH, "//div[@class='cafe-c-input-number']/a[2]"))) addbtn.click() except: raise Exception("***错误:票数增加失败***") if buybutton_text == "即将开抢" or buybutton_text == "即将开售": self.status = 2 self.driver.refresh() print('---尚未开售,刷新等待---') continue elif buybutton_text == "立即预订": add_ticket() buybutton.click() self.status = 3 elif buybutton_text == "立即购买": add_ticket() buybutton.click() self.status = 4 elif buybutton_text == "选座购买": # 选座购买暂时无法完成自动化 # buybutton.click() self.status = 5 print("###请自行选择位置和票价###") break elif buybutton_text == "提交缺货登记": print('###抢票失败,请手动提交缺货登记###') break except: self.driver.refresh() print("下一次")
import os import pickle import time from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.action_chains import ActionChains brower = webdriver.Chrome() wait = WebDriverWait(brower, 10) def getCookies(): # get login taobao cookies url = "https://www.kjjxjy.com/center/myStudy/goods/detail?year=2019" brower.get("https://www.kjjxjy.com/index/") while True: print("Please login in") time.sleep(3) # if login in successfully, while brower.current_url == url: Cookies = brower.get_cookies() brower.quit() cookies = {} for item in Cookies: cookies[item['name']] = item['value'] outputPath = open('jxjycookies.pickle', 'wb') pickle.dump(cookies, outputPath) outputPath.close() return cookies
def test_clickable_elements(self): driver = self.driver sleep(2) for i in range(0, 2): # radio animal WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='00000008_" + str( i) + "']"))).click() WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='00000008_" + str( i) + "' and @aria-checked='true']"))) for i in range(0, 2): # radio river WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='0000000a_" + str( i) + "']"))).click() WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='0000000a_" + str( i) + "' and @aria-checked='true']"))) for i in range(0, 4): # checkbox inventions WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='0000000c_" + str( i) + "']"))).click() WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='0000000c_" + str( i) + "' and @aria-checked='true']"))) for i in range(0, 3): # checkbox program WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='0000000e_" + str( i) + "']"))).click() WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='0000000e_" + str( i) + "' and @aria-checked='true']"))) for i in range(0, 2): # checkbox food WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='00000010_" + str( i) + "']"))).click() WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//label[@for='00000010_" + str( i) + "' and @aria-checked='true']"))) WebDriverWait(driver, 5).until( EC.visibility_of_element_located((By.XPATH, "//input[@type='email']"))).send_keys('*****@*****.**') driver.find_element_by_tag_name('html').send_keys(Keys.TAB)
def search_elements(self,loc,to=10): return WebDriverWait(self.driver,timeout=to).until(lambda x : x.find_elements(*loc))
right = code_element.size['width'] + left height = code_element.size['height'] + top im = Image.open("E:/Imooc/imooc2.png") img = im.crop((left, top, right, height)) img.save("E:/Imooc/imooc3.png") r = ShowapiRequest("http://route.showapi.com/932-2", "164135", "7eb4c4b4448e4fe3a9dad2cd2c8e0e02") r.addBodyPara("length", "5") r.addBodyPara("specials", "false") r.addBodyPara("secure", "false") r.addBodyPara("image", r"E:/imooc1.png") res = r.post() print(res.text) text = res.json()['showapi_res_body']['code'] print(text) time.sleep(2) driver.find_element_by_id('captcha_code').send_keys(text) # for i in range(5): # user_email = ''.join(random.sample('1234567890abcdefg', 6)) # print(user_email) locator = (By.CLASS_NAME, "controls") WebDriverWait(driver, 2).until(EC.visibility_of_element_located(locator)) print(email_element.get_attribute('placeholder')) email_element.send_keys("*****@*****.**") print(email_element.get_attribute('value')) time.sleep(5) driver.close()
def __init__(self, driver): self.driver = driver self.wait = WebDriverWait(self.driver, 15) self.helper = Helper(self.driver)
class AccountHttp(object): def __init__(self): self.url = 'https://weixin.sogou.com/weixin?type=1&s_from=input&query={}&ie=utf8&_sug_=n&_sug_type_=' self.account = '' self.name = '' self.s = requests.session() self.s.keep_alive = False # 关闭多余连接 self.s.adapters.DEFAULT_RETRIES = 5 # 增加重连次数 self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36', } self.cookies = {} # chrome_options = webdriver.ChromeOptions() # chrome_options.add_argument('--headless') # # 多线程driver共享 self.driver = driver self.wait = WebDriverWait(self.driver, 4) self.rcon = redis.StrictRedis(db=8) self.queue = 'analyse' self.status = 4 @async def listen_task(self): while True: try: if not self.driver: chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') self.driver = webdriver.Chrome( chrome_options=chrome_options) account_char = self.rcon.brpop(self.queue, 0)[1] self.name = account_char.decode(encoding="utf-8") self.run() log("消耗一个account") except Exception as e: log('error', '重启', e) if self.driver: self.driver.quit() continue def send_info(self, info): loop_count = 0 while True: loop_count += 1 if loop_count > 3: break send_info = dict() send_info['account'] = info.get('account', '') send_info['name'] = info.get('name', '') # send_info['imageUrl'] = path send_info['imageUrl'] = info.get('imageUrl', '') send_info['message'] = info.get('message', True) send_info['feature'] = info.get('features', '') send_info['certification'] = info.get('certified', '') send_info['status'] = 3 send_url = 'http://58.56.160.39:38012/MediaManager/api/weixinInfo/add' r = requests.post(send_url, data=json.dumps(send_info)) if r.status_code == 200: log("发送weixininfo成功") break def handle_img(self, img_b, image_id, info, path): url_img = 'http://47.99.50.93:8009/SaveImage' data_img = {'content': base64.b64encode(img_b), 'account_id': image_id} r = requests.post(url_img, data=data_img) log('头像上传:', r.status_code) # 更新数据库 config_mysql_old = get_mysql_old() db = pymssql.connect(**config_mysql_old) cursor = db.cursor() try: sql_insert = """UPDATE WXAccount SET ImageUrl='{}' WHERE ID='{}'""".format( path, image_id) cursor.execute(sql_insert) db.commit() log('更新数据成功', info.get('name')) except Exception as e: log('更新数据错误', e) db.rollback() def uploads_account_info(self, e): info = dict() info['Name'] = e(".tit").text() info['Account'] = self.name show_list = e("dl") features, certified = '', '' for show in show_list: if '功能介绍' in pq(show).text(): features = pq(show).text().replace('功能介绍:\n', '') if '认证' in pq(show).text(): certified = pq(show).text().split('\n')[-1] info['Feature'] = features info['Certification'] = certified info['message'] = True info['status'] = 1 # 获取头像二进制 img_find = e(".img-box").find('img').attr('src') url_img_get = 'http:' + img_find info['ImageUrl'] = url_img_get return info # self.send_info(info) def account_homepage(self, choice=''): # 搜索并进入公众号主页 count = 0 while True: count += 1 if count > 3: break log('start', self.name) search_url = self.url.format(self.name) resp_search = self.s.get(search_url, headers=self.headers, cookies=self.cookies) e = pq(resp_search.text) log(e('title').text()) if '搜狗' not in e('title').text(): log('初始化session') self.s = requests.session() if self.name in e(".info").eq(0).text(): if choice: account_info = self.uploads_account_info(e) return account_info account_link = e(".tit").find('a').attr('href') # self.uploads_account_info(e) homepage = self.s.get(account_link, cookies=self.cookies) if '<title>请输入验证码 </title>' in homepage.text: self.crack_sougou(account_link) homepage = self.s.get(account_link, cookies=self.cookies) return homepage.text, self.name elif len(e(".tit").eq(0).text()) > 1: log("不能匹配正确的公众号: {}".format(self.name)) break if '相关的官方认证订阅号' in resp_search.text: log("搜狗找不到该公众号: {}".format(self.name)) return '搜狗无该账号', self.name else: log('url cookie 失效', search_url) try_count = 0 while True: try_count += 1 self.crack_sougou(search_url) if '搜公众号' in self.driver.page_source: log('------cookies更新------') cookies = self.driver.get_cookies() new_cookie = {} for items in cookies: new_cookie[items.get('name')] = items.get('value') self.cookies = new_cookie log('------cookies已更新------', self.cookies) break elif try_count > 6: log("浏览器验证失败") break log("验证完毕") time.sleep(2) # 被跳过的公众号要不要抓取 大概 4次 continue def set_name(self): url = 'http://124.239.144.181:7114/Schedule/dispatch?type=8' resp = self.s.get(url) data_json = resp.text.get('data') if len(data_json) == 0: return '' data = json.loads(data_json) self.name = data.get('name') def urls_article(self, html): items = re.findall('"content_url":".*?,"copyright_stat"', html) urls = [] for item in items: url_last = item[15:-18].replace('amp;', '') url = 'https://mp.weixin.qq.com' + url_last # 部分是永久链接 if '_biz' in url_last: url = re.search('http://mp.weixin.qq.*?wechat_redirect', url_last).group() urls.append(url) continue # 可能匹配过多,再次匹配 if 'content_url' in url: item = re.search('"content_url":".*?wechat_redirect', url).group() url = item[15:].replace('amp;', '') urls.append(url) return urls def emotion_judge(self, content): key_words_list = [] seg_list = jieba.cut(content) for s in seg_list: if re.search('[\u4e00-\u9fff]+', s): key_words_list.append(s) # log(key_words_list) with open('positive.txt', 'r', encoding='utf-8') as f: positive = f.read() with open('nagetive.txt', 'r', encoding='utf-8') as f: nagetive = f.read() # key_list = list(key_words_counter) count_positive = 0 count_nagetive = 0 for key in key_words_list: if key in positive.split('\n'): count_positive += 1 if key in nagetive.split('\n'): count_nagetive += 1 return count_positive, count_nagetive def send_result(self): # 向前端发送成功请求 # article_detaile = db['newMedia'].find_one({'Account': self.name}) try: log("发前端") account_id = hash_md5(self.name) status_url = 'http://58.56.160.39:38012/MediaManager/api/drafts/updateAnalysisStatusByAnalysisId' params = { 'type': 3, 'analysisId': account_id, # 3 成功 4 失败 'status': self.status, } r = requests.get(status_url, params=params) log('status_code', r.status_code) except Exception as e: log("发送前端结果错误", e) def run(self, name, choice=''): # choice 选择是返回账号主体信息(True) or 分析结果('') self.name = name html_account = self.account_homepage(choice) # 返回账户信息 if choice: return html_account if html_account: html, account_of_homepage = html_account else: # self.send_result() return log('start 公众号: ', self.name) urls_article = self.urls_article(html) account = Account() account.name = self.name account.account = account_of_homepage account.get_account_id() articles = [] backpack_list = [] positive_article = 0 nagetive_article = 0 for page_count, url in enumerate(urls_article): # if page_count > 2: # break article = Article() log('url:', url) article.create(url, self.name) log('文章标题:', article.title) log("第{}条".format(page_count)) # 超过7天不管 if article.time: article_date = datetime.datetime.fromtimestamp( int(article.time[:-3])) day_diff = datetime.datetime.now().date() - article_date.date() if day_diff.days > 6: break # 统计文章正负面 count_positive, count_nagetive = self.emotion_judge( article.content) if count_positive > count_nagetive: positive_article += 1 else: nagetive_article += 1 entity = JsonEntity(article, account) backpack = Backpack() backpack.create(entity) backpack_list.append(backpack.create_backpack()) # 所有文章 article_info = backpack.to_dict() articles.append(article_info) log('所有文章抓取完毕') content_all_list = '' for article in articles: content_all_list += article.get('Content') log('文章长度', len(content_all_list)) # 分词处理 key_words_list = [] data = { "texts": [content_all_list], } log('请求分词') response = requests.post(url=GETNER_API_URL, data=data, timeout=180) ner_result = response.json().get('rst')[0] if ner_result.get('status') == 'success': org_dic = ner_result.get('ner').get('ORG') loc_dic = ner_result.get('ner').get('LOC') per_dic = ner_result.get('ner').get('PER') if org_dic: for i in org_dic.items(): key_words_list.append(i) if loc_dic: for i in loc_dic.items(): key_words_list.append(i) if per_dic: for i in per_dic.items(): key_words_list.append(i) # 返回前20个出现频率最高的词 key_words = dict() key_words['list'] = [] key_words_list = sorted(key_words_list, key=lambda x: x[1], reverse=True)[:21] for k in key_words_list: key_words['list'].append({"times": k[1], "keyword": k[0]}) log('分词完成') # 处理文章 result = handle(articles) result['KeyWord'] = key_words result['ArtPosNeg'] = { 'Indicate': { 'Positive': positive_article, 'Negative': nagetive_article } } result['Success'] = True result['Account'] = self.name result['Message'] = 'Successful' mongo_conn_sentiment().update_one({'Account': self.name}, { '$set': { 'Addon': time_strftime(), 'Message': 'Successful', 'Data': result } }, upsert=True) log('{} 抓取完成'.format(self.name)) return result def crack_sougou(self, url): log('------开始处理未成功的URL:{}'.format(url)) if re.search('weixin\.sogou\.com', url): log('------开始处理搜狗验证码------') self.driver.get(url) time.sleep(2) if '搜公众号' in self.driver.page_source: for i in range(30): self.driver.get(url) log('浏览器页面正常') if '搜公众号' not in self.driver.page_source: break try: img = self.wait.until( EC.presence_of_element_located((By.ID, 'seccodeImage'))) log('------出现验证码页面------') location = img.location size = img.size left = location['x'] top = location['y'] right = location['x'] + size['width'] bottom = location['y'] + size['height'] screenshot = self.driver.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)) captcha = screenshot.crop((left, top, right, bottom)) captcha_path = os.path.join(IMAGE_DIR, CAPTCHA_NAME) captcha.save(captcha_path) try: captch_input = '' files = { 'img': (CAPTCHA_NAME, open(captcha_path, 'rb'), 'image/png', {}) } res = requests.post(url=GetCaptcha_url, files=files) res = res.json() if res.get('Success'): captch_input = res.get('Captcha') except Exception as e: log('搜狗验证码验证错误', e) with open(captcha_path, "rb") as f: filebytes = f.read() captch_input = captch_upload_image(filebytes) log('------验证码:{}------'.format(captch_input)) if captch_input: input_text = self.wait.until( EC.presence_of_element_located( (By.ID, 'seccodeInput'))) input_text.clear() input_text.send_keys(captch_input) submit = self.wait.until( EC.element_to_be_clickable((By.ID, 'submit'))) submit.click() time.sleep(2) try: if '搜公众号' not in self.driver.page_source: log('验证失败') return log('------验证码正确------') except: log('--22222222----验证码输入错误------') except Exception as e: log(e) log('------未跳转到验证码页面,跳转到首页,忽略------') elif re.search('mp\.weixin\.qq\.com', url): log('------开始处理微信验证码------') cert = random.random() image_url = 'https://mp.weixin.qq.com/mp/verifycode?cert={}'.format( cert) respones = self.s.get(image_url, cookies=self.cookies) captch_input = captch_upload_image(respones.content) log('------验证码:{}------'.format(captch_input)) data = {'cert': cert, 'input': captch_input} self.s.post(image_url, cookies=self.cookies, data=data) log('------微信验证码处理完成------')
def verify_link_presence(self, locator_type, locator): wait = WebDriverWait(self.driver, 10) wait.until(ec.presence_of_element_located((locator_type, locator)))
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome( executable_path=r'D:\Naveen\Selenium\chromedriver_win32\chromedriver.exe') driver.maximize_window() driver.get(r'http://the-internet.herokuapp.com/dynamic_controls') driver.find_element_by_xpath("//button[text()='Remove']").click() wait = WebDriverWait(driver, 5) message = wait.until(EC.visibility_of_element_located((By.ID, 'message'))).text assert message == "It's gone!" try: driver.find_element_by_id('checkbox') except NoSuchElementException as err: pass
def get_user_nickname(self): #等待页面元素出现 WebDriverWait(self.driver, 15, 0.5).until( EC.visibility_of_all_elements_located( (By.XPATH, HL.userinfo_link_xpath))) return self.driver.find_element_by_xpath(HL.userinfo_link_xpath).text
class DriverInfo: def __init__(self, driver): self.driver = driver self.wait = WebDriverWait(self.driver, 15) self.helper = Helper(self.driver) def enter_driver_info(self, state_id): self.wait.until( EC.presence_of_element_located( (By.CSS_SELECTOR, '.is-current a[href="#/drivers/driver_summary"]'))) self.enter_firstname().send_keys('Carmine') self.enter_lastname().send_keys('Falcone') self.enter_dob().send_keys(7777) self.select_gender('1').click() # for CA state if state_id == 'CA': self.wait.until( EC.element_to_be_clickable( (By.CSS_SELECTOR, 'label[data-field="was_license_suspended"]:nth-of-type(1)' ))) self.select_license_status().click() self.select_license_age('18') else: pass # for CA state if state_id == 'CA': pass else: self.wait.until( EC.element_to_be_clickable( (By.CSS_SELECTOR, 'label[data-analytics-label="excellent"]'))) self.select_credit_score('Good').click() self.wait.until( EC.element_to_be_clickable( (By.CSS_SELECTOR, 'label[data-analytics-label="some_college_no_degree"]'))) self.select_level_of_education('Doctoral').click() self.save_and_continue_button().click() def enter_firstname(self): firstname = self.driver.find_element_by_css_selector( 'input[name="first_name"]') return firstname def enter_lastname(self): lastname = self.driver.find_element_by_css_selector( 'input[id="last_name"]') return lastname def enter_dob(self): dob = self.driver.find_element_by_css_selector( 'input[id="driver_info_dob"]') return dob def select_gender(self, gender_type): gender = self.driver.find_element_by_css_selector( 'label[data-field="gender"]:nth-of-type(' + gender_type + ')') return gender def select_credit_score(self, credit_score): credit_scores = self.driver.find_elements_by_css_selector( 'label[data-field="credit_score"]') for credit in credit_scores: if credit.text == credit_score: return credit def select_level_of_education(self, education): education_level = self.driver.find_elements_by_css_selector( 'label[data-field="education"]') for edu in education_level: if edu.text == education: return edu def select_license_status(self): license_status = self.driver.find_element_by_css_selector( 'label[data-field="was_license_suspended"]:nth-of-type(1)') return license_status def select_license_age(self, driver_license_age): license_age = self.driver.find_elements_by_css_selector( 'div[id="driver_info_license_age_container"] label') for age in license_age: if age.text == driver_license_age: return age def save_and_continue_button(self): driver_info_btn = self.driver.find_element_by_css_selector( 'button[id="driver_info_continue_btn"]') return driver_info_btn
def wait_for_element_visibility(self, element_id, delay=5): WebDriverWait(self.driver, delay).until( EC.visibility_of_element_located((By.ID, element_id)))
def base_find_element(self, loc, timeout=10, poll=0.5): return WebDriverWait( self.driver, timeout=timeout, poll_frequency=poll).until(lambda x: x.find_element(*loc))
def selenium(keywords_1, index, time_1, time_2, head, type): """网页获取""" # 无弹窗 path = "E:\Firefox\Download" if not os.path.exists(path): os.makedirs(path) profile = webdriver.FirefoxProfile() profile.set_preference('browser.download.dir', path.strip('\u202a')) profile.set_preference('browser.download.folderList', 2) profile.set_preference('browser.download.manager.showWhenStarting', False) profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/zip,application/octet-stream') #无图 profile.set_preference('browser.migration.version', 9001) profile.set_preference('permissions.default.image', 2) #无头 ops = Options() if head == 1: ops.add_argument('--headless') """网页获取""" browser = webdriver.Firefox(firefox_profile=profile, options=ops) actions = ActionChains(browser) try: browser.get('http://wenshu.court.gov.cn') wait = WebDriverWait(browser, 30) send = wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="_view_1540966814000"]/div/div[1]/div[2]/input'))) send.send_keys(keywords_1) button = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="_view_1540966814000"]/div/div[1]/div[3]'))) button.click() try: """优化为::高级检索 """ click1 = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="_view_1545034775000"]/div/div[1]/div[1]'))) click1.click() #发送country, """ send1 = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="s2"]'))) send1.send_keys(country) """ # 具体案由选择 """案件类型选择:刑事案件""" if type == 1: click_type = wait.until( EC.element_to_be_clickable((By.XPATH, '//*[@id="s8"]'))) click_type.click() click_this = wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="gjjs_ajlx"]/li[3]'))) click_this.click() else: click_ = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="selectCon_other_ajlx"]'))) click_.click() click_type = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="gjjs_ajlx"]/li[4]'))) click_type.click() if type == 1: """审判程序选择:一审程序""" click_type = wait.until( EC.element_to_be_clickable((By.XPATH, '//*[@id="s9"]'))) click_type.click() click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="0201_anchor"]'))) click_this.click() else: """审判程序选择:一审程序""" click_type = wait.until( EC.element_to_be_clickable((By.XPATH, '//*[@id="s9"]'))) click_type.click() click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="0301_anchor"]'))) click_this.click() """案由选择:选择案由、刑事案由、具体案由、确定的案由""" try: # 案由选择 click_type = wait.until( EC.element_to_be_clickable((By.XPATH, '//*[@id="s16"]'))) click_type.click() click_t = wait.until( EC.element_to_be_clickable((By.XPATH, '//*[@id="1"]/i'))) click_t.click() if type == 1: # 贪污渎职罪案由选择 # 具体案由选择2,15,161,199,212,341,363,376,408,440 # 小下拉框 if 2 <= int(index) < 15: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="2"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) # 贪污://*[@id="363_anchor"] click_crime.click() elif 15 <= index < 58: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="15"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif 58 <= index < 161: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="58"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif 161 <= index < 199: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="161"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif 199 <= index < 212: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="199"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif 212 <= index < 341: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="212"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif 341 <= index < 363: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="341"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif 363 <= index < 376: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="363"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif 376 <= index < 408: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="376"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif 408 <= index < 440: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="408"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() elif index == 440: click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="440"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() else: print("民事案例检索中") click_this = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="9000"]/i'))) click_this.click() click_crime = wait.until( EC.element_to_be_clickable( (By.XPATH, '//*[@id="%d_anchor"]' % index))) click_crime.click() except: pass """审判日期:2019""" time_send = wait.until( EC.element_to_be_clickable((By.XPATH, '//*[@id="cprqStart"]'))) time_send.click() # 开始日期,直接sendkeys不行 actions.send_keys_to_element(time_send, time_1).perform() time_send_end = wait.until( EC.element_to_be_clickable((By.XPATH, '//*[@id="cprqEnd"]'))) time_send_end.click() # time_send_end.clear() actions.send_keys_to_element(time_send_end, time_2).perform() # 结束日期 """检索确定""" button_1 = wait.until( EC.element_to_be_clickable((By.XPATH, '//*[@id="searchBtn"]'))) button_1.click() """再次点击限制条件为省市,限制条件下循环""" #xpath制造 a = ['//*[@id="%d_anchor"]' % (i * 100) for i in range(1, 10)] b = [ '//*[@id="{name}00_anchor"]'.format(name=chr(x).upper()) for x in range(ord('a'), ord('x') + 1) ] list_xpath = a + b if '//*[@id="W00_anchor"]' in list_xpath: list_xpath.remove('//*[@id="W00_anchor"]') for xpath_ in list_xpath: try: time.sleep(1) browser.find_element_by_xpath(xpath_) except: pass else: button_xpath = wait.until( EC.element_to_be_clickable((By.XPATH, xpath_))) button_xpath.click() browser.refresh() """文书数量:15""" button_ = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="left_7_3"]/div/select'))) button_.click() button_ = wait.until( EC.element_to_be_clickable( (By.XPATH, '//div[@class="left_7_3"]/div/select/option[3]'))) button_.click() time.sleep(4) """目的:减少遍历次数,进行页数遍历""" condition = browser.find_element_by_xpath( '//div[@class="LM_con clearfix"]/div[@class="fr con_right"]/span' ) # return (condition.text) # 不能直接//text()原因不明 conditions = math.ceil(int(condition.text) / 15) # 最长12,最短6 if int(condition.text) == 0 or conditions == 0: """取消省份限制""" button_xpath = wait.until( EC.element_to_be_clickable( (By.XPATH, '//p[@data-key="s33"]/i'))) button_xpath.click() time.sleep(1) elif int(conditions) > 40: # condition本身已经除了15 with open('裁判文书超过600页.txt', 'a+', encoding='utf-8') as file: file.write('出现超过600条的裁判文书,其所在区域为:' + ',其数量为:' + str(condition.text) + '\n') elif 0 < int(conditions) <= 40: for index in range(conditions): # print('运行至遍历处。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。') try: '''全选的点击''' click_1 = wait.until( EC.presence_of_element_located(( By.XPATH, '//div[@class="LM_tool clearfix"]/div[4]/a[1]/label' ))) click_1.click() '''批量下载的点击''' click_2 = wait.until( EC.presence_of_element_located(( By.XPATH, '//html/body/div/div[4]/div[2]//div[@class="LM_tool clearfix"]/div[4]/a[3]' ))) click_2.click() """下一页""" button_ = wait.until( EC.presence_of_element_located(( By.XPATH, '//div[@class="left_7_3"]/a[last()]'))) button_.click() time.sleep(4) message = "正在进行第%d页爬取中-----------------------" % index + "xpath为: {}".format( xpath_) yield message except Exception as e: print("此处退出-------3-----------") print("出现错误: ", e, xpath_) else: """取消省份限制""" button_xpath = wait.until( EC.element_to_be_clickable( (By.XPATH, '//p[@data-key="s33"]/i'))) button_xpath.click() time.sleep(1) """取消省份限制""" button_xpath = wait.until( EC.element_to_be_clickable( (By.XPATH, '//p[@data-key="s33"]/i'))) button_xpath.click() time.sleep(1) except Exception as e: print("第一个", e) pass except Exception as e: print("第二个", e) pass
def test_sIPPermissaoUsuario2UnTeste2x50(self): self.driver.get( "http://sei3.nuvem.gov.br//sip/login.php?sigla_orgao_sistema=ME&sigla_sistema=SIP" ) self.driver.find_element(By.ID, "txtUsuario").send_keys("teste") self.driver.find_element(By.ID, "pwdSenha").click() self.driver.find_element(By.ID, "pwdSenha").send_keys("teste") self.driver.find_element(By.ID, "sbmLogin").click() self.vars["iLoop"] = self.driver.execute_script("return 2") for i in range(0, 50): self.vars["iLoop"] = self.driver.execute_script( "return arguments[0] +1", self.vars["iLoop"]) WebDriverWait(self.driver, 30000).until( expected_conditions.visibility_of_element_located( (By.XPATH, "//ul[@id=\'main-menu\']/li[1]/a"))) self.driver.find_element( By.XPATH, "//ul[@id=\'main-menu\']/li[1]/a").click() print(str("un6")) self.driver.find_element(By.LINK_TEXT, "Nova").click() WebDriverWait(self.driver, 30000).until( expected_conditions.element_to_be_clickable( (By.ID, "selOrgaoSistema"))) self.driver.find_element(By.ID, "selOrgaoSistema").click() dropdown = self.driver.find_element(By.ID, "selOrgaoSistema") dropdown.find_element(By.XPATH, "//option[. = 'ME']").click() self.driver.find_element(By.ID, "selOrgaoSistema").click() WebDriverWait(self.driver, 30000).until( expected_conditions.element_to_be_clickable( (By.ID, "selSistema"))) dropdown = self.driver.find_element(By.ID, "selSistema") dropdown.find_element(By.XPATH, "//option[. = 'SEI']").click() self.driver.find_element(By.ID, "selSistema").click() WebDriverWait(self.driver, 30000).until( expected_conditions.element_to_be_clickable( (By.ID, "selOrgaoUnidade"))) self.driver.find_element(By.ID, "selOrgaoUnidade").click() dropdown = self.driver.find_element(By.ID, "selOrgaoUnidade") dropdown.find_element(By.XPATH, "//option[. = 'ME']").click() self.driver.find_element(By.ID, "selOrgaoUnidade").click() WebDriverWait(self.driver, 30000).until( expected_conditions.element_to_be_clickable( (By.ID, "selUnidade"))) dropdown = self.driver.find_element(By.ID, "selUnidade") dropdown.find_element(By.CSS_SELECTOR, "*[value='110000003']").click() self.driver.find_element(By.ID, "selUnidade").click() self.driver.find_element(By.ID, "selOrgaoUsuario").click() dropdown = self.driver.find_element(By.ID, "selOrgaoUsuario") dropdown.find_element(By.XPATH, "//option[. = 'ME']").click() self.driver.find_element(By.ID, "selOrgaoUsuario").click() self.driver.find_element(By.ID, "txtUsuario").click() print(str("aqui")) self.driver.find_element(By.ID, "txtUsuario").clear() self.driver.find_element( By.ID, "txtUsuario").send_keys("usuario" + str(self.vars["iLoop"])) # self.driver.save_screenshot("teste.png") WebDriverWait(self.driver, 30000).until( expected_conditions.visibility_of_element_located( (By.CSS_SELECTOR, "b:nth-child(1)"))) element = self.driver.find_element(By.CSS_SELECTOR, "b:nth-child(1)") actions = ActionChains(self.driver) actions.move_to_element(element).click_and_hold().perform() element = self.driver.find_element(By.ID, "divInfraAreaDados") actions = ActionChains(self.driver) actions.move_to_element(element).release().perform() self.driver.find_element(By.CSS_SELECTOR, "body").click() self.driver.find_element(By.ID, "selPerfil").click() dropdown = self.driver.find_element(By.ID, "selPerfil") dropdown.find_element(By.XPATH, "//option[. = 'Básico']").click() self.driver.find_element(By.NAME, "sbmCadastrarPermissao").click()
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait driver = webdriver.Chrome() driver.maximize_window() driver.get('https://www.jd.com/') #显式等待 wait = WebDriverWait(driver, 10, 1) wait.until(EC.presence_of_element_located((By.ID,'key'))) driver.find_element(By.ID , 'key').send_keys("华为") driver.find_element(By.ID , 'key').send_keys(Keys.ENTER) time.sleep(3) driver.quit()
def __init__(self, driver): self.driver = driver self.wait = WebDriverWait(self.driver, 20)
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from selenium.common.exceptions import TimeoutException from urllib.parse import quote broswer=webdriver.Chrome() wait=WebDriverWait(broswer,20) KEYWORD="ipad" def index_page(page): print("正在爬取第",page,"页") try: url="https://s.taobao.com/search?q="+quote(KEYWORD) broswer.get(url) if page>1: input=wait.until( EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager div.form>input'))) submit=wait.until( EC.element_to_be_clickable((By.CSS_SELECTOR,'#mainsrp-pager div.form>span.btn.J_Submit'))) input.clear() input.send_keys(page) submit.click() wait.until( EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager li.item.active>span'),str(page))) wait.until( EC.presence_of_element_located((By.CSS_SELECTOR,'.m-itemlist.item.item'))) get_products()
def _register_ted(driver, job_data): time.sleep(1) navigation.switch_to_frame(driver, "CORPO") time.sleep(1) small_wait = WebDriverWait(driver, 8) logger.info("Filling in TED form...") try: # TEF amount _fill_input(small_wait, '//input[@id="dia"]', job_data['day']) _fill_input(small_wait, '//input[@id="mes"]', job_data['month']) _fill_input(small_wait, '//input[@id="ano"]', job_data['year']) _fill_input(small_wait, '//input[@name="valor" and @size="16"]', job_data['amount']) except TimeoutException as ex: logger.critical('Timeout when filling in form: {}'.format(str(ex))) return operation_codes.OP_TIMEOUT # If checking account, must select "Credit on account" purpose of the operation if job_data['account_type'] == 'CH': try: # Operation code: Credit on account credit_op_xpath = '//select[@id="Finalidade"]/option[@value="9"]' op_element = driver.find_element(By.XPATH, credit_op_xpath) op_element.click() except NoSuchElementException: logger.critical("Unable to select operation code element : {}".format(credit_op_xpath)) return operation_codes.OP_FAILED # navigation.switch_to_frame(driver, "CORPO") logger.info("Locating submit button...") submit_xtag = '//input[@name="Incluir" and @type="button"]' try: submit_btn = small_wait.until(EC.element_to_be_clickable((By.XPATH, submit_xtag))) hover = ActionChains(driver).move_to_element(submit_btn) hover.perform() logger.info("Submitting TED...") click_action = ActionChains(driver).click(submit_btn) click_action.perform() except TimeoutException: logger.critical("Unable to locate submit button: {}".format(submit_xtag)) return operation_codes.OP_FAILED time.sleep(3) logger.info("TED submitted, checking if operation was approved...") success_xpath = '//*[contains(text(), "sucesso")]' try: small_wait.until(EC.visibility_of_element_located((By.XPATH, success_xpath))) except TimeoutException as ex: logger.critical("Unable to find operation approval status!") return operation_codes.OP_FAILED logger.info("TEF successfully registered!") return operation_codes.OP_SUCCESS
def find_by_xpath(self, xpath): """Return the element by its xpath""" return WebDriverWait(self.driver, self.ui_wait_time).until( EC.visibility_of_element_located((By.XPATH, xpath)))
#Setup Webdriver, navigate to a page and take a screenshot driver = webdriver.Firefox() width = 800 height = 600 driver.set_window_size(width, height) driver.get("https://google.com") screenshot = driver.get_screenshot_as_png() #Send the screenshot to AWS Textract client = boto3.client('textract') response = client.detect_document_text(Document={'Bytes': screenshot}) blocks = response['Blocks'] #Analyse response and find the location for a given text string x, y = find_average_pixel(width, height, blocks, "Store", "WORD") #Tell the browser to click the location via Javascript script = 'el = document.elementFromPoint(%d, %d); el.click();' % (x, y) driver.execute_script(script) #Check to see if we successfully navigated to the correct page try: WebDriverWait(driver, 10).until( expected_conditions.title_contains("Google Store")) print("Yay it worked") except TimeoutException: print("It didn't work") driver.close()