def handle_room_tag(driver: webdriver, comm: str): """ 进入每一个直播, 并插播广告 写入日志 """ # 将所有标签统计出来 driver.execute_script(Order.page_end.value) sum_room = driver.find_elements_by_class_name(Order.room_tag.value) i = 0 while i < len(sum_room): try: _into_room_handle(driver, comm, i) except ElementClickInterceptedException: move_down(driver) _into_room_handle(driver, comm, i) i += 1 try: move_down(driver) tag = driver.find_element_by_css_selector(Order.page_down.value) if tag: time.sleep(1) tag.click() driver.implicitly_wait(5) handle_room_tag(driver, comm) except NoSuchElementException: print("finish") driver.close() return
def citidownload(wd: webdriver, orga, ptrack, pacc=None): accountNamesWords = ['interest checking', 'savings plaus'] re1 = re.compile('\s*\-\s*') try: print('-----------citidownload') wd.implicitly_wait(10) # WebDriverWait(wd, 10).until(EC.presence_of_element_located((By.XPATH, ''))) webArchs = wd.find_elements(By.TAG_NAME, 'a') print('-----------citidownload, 1') for arch in webArchs: print('-----------citidownload, 2') stext = arch.get_attributes('text') print('-----------citidownload, 3, ', stext) satext = re1.split(stext.strip()) if len(satext) != 2: continue if satext[0] not in accountNamesWords: continue if len(satext[1]) != 4: continue try: last4digis = int(satext[0]) except Exception as e: continue href = arch.get_attributes('href') if href.find('JFP_TOKEN') <= 10: continue arch.click() dropDowns = wd.find_elements( (By.XPATH, '//div[@class="dropdown"]/svg')) for dropDown in dropDowns: lastDaysWe = dropDown.find_element( (By.XPATH, 'preceding-sibling::::div')) lastDays = lastDays.get_attributes('text') if lastDays[-4:] == 'days' or lastDays[ -6:] == 'months' or lastDays == 'specific date' or lastDays == 'customdaterange': if lastDays != 'custom date range': dropDown.click() break startDateEle = None endDateEle = None downloadEle = None downloadEle.click() QFXEles = wd.find_elements((By.XPATH, '//input[@type="radio"]')) for aQFX in QFXEles: aQFXText = aQFX.get_attributes('text') if aQFXText[:3] == 'QFX': btns = wd.find_elements( (By.XPATH, '//button[@type="button"]')) for btn in btns: btnTxt = btn.get_attributes('text') if btnTxt.lower() == 'download': btnTxt.click() closeEle = wd.find_elements((By.XPATH, '//')) closeEle.click() break #loop Download directory and find the newest one and move to data directory except Exception as e: tb.print_tb(e.__traceback__) print(e)
def symbol_to_frames(symbol: str, browser_object: webdriver) -> DataFrame: """ Takes in the symbol and returns floorsheet dataframe """ search_url: str = f"{BASE_URL}{symbol}" browser_object.get(search_url) browser_object.implicitly_wait(IMP_DELAY) # Click on the nav button browser_object.find_element_by_id("navFloorSheet").click() wait() intent_block = browser_object.find_element_by_id( "ctl00_ContentPlaceHolder1_CompanyDetail1_divDataFloorsheet") frame_block = block_to_frame(intent_block) next_button = browser_object.find_element_by_xpath( "//a[@title='Next Page']") pages = [] while get_pages(intent_block)["current_page"] <= get_pages( intent_block)["last_page"] and get_pages( intent_block)["current_page"] not in pages: pages.append(int(get_pages(intent_block)["current_page"])) next_button.click() wait() next_button = browser_object.find_element_by_xpath( "//a[@title='Next Page']") intent_block = browser_object.find_element_by_id( "ctl00_ContentPlaceHolder1_CompanyDetail1_divDataFloorsheet") inter_frame = block_to_frame(intent_block) frame_block = pd.concat([frame_block, inter_frame], axis=0) pages = [] return frame_block
def waitforelementbyxpath(self, driver: webdriver, xpath, seconds): print("Waiting for element") for i in range(seconds): try: driver.find_element(By.XPATH, xpath) break except NoSuchElementException: driver.implicitly_wait(1)
def waitforelementbytext(self, driver: webdriver, text, seconds): print("Waiting for element") for i in range(seconds): try: driver.find_element(By.LINK_TEXT, text) break except NoSuchElementException: driver.implicitly_wait(1)
def buy(driver: webdriver): url = 'https://market.m.taobao.com/app/sj/shop-membership-center/pages/index?spm=a1z10.4-b-s.w5003-22059585455.1.7f86274aL8QKmA' \ '&wh_weex=true&wx_navbar_transparent=true&sellerId=2360209412&scene=taobao_shop' btn_buy = "//span[contains(text(),'1积分享兰蔻菁纯宝石唇膏 02')]/../following-sibling::div[1]//div[@class='gift-act-btn']" btn_word = "//span[contains(text(),'1积分享兰蔻菁纯宝石唇膏 02')]/../following-sibling::div[1]//span[@class='gift-act-btn-text']" btn = "//div[@class='btnWarp']" word = "//span[@class='btn']" count = 0 login_time = "2020-06-10 13:23:00" buy_time = "2020-06-10 13:23:55" isFirst = True wait = WebDriverWait(driver, 10, 0.5) while True: if datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') < buy_time: # 提前登录 if isFirst and datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S.%f') >= login_time: driver.maximize_window() driver.get(url) driver.implicitly_wait(2) login(driver) isFirst = False continue else: # 如果连续抢60次都抢不到 那么就放弃吧 count += 1 if count > 60: break print("即将开始抢购...") print(datetime.datetime.now()) # 重新进页面 driver.refresh() # 开始页面的唇膏抢购按钮 buy_btn = wait.until( lambda driver: driver.find_element_by_xpath(btn_buy)) # 开始页面的唇膏抢购按钮上的文字 word_btn = wait.until( lambda driver: driver.find_element_by_xpath(btn_word)) if word_btn.text == '立即兑换': # 如果抢购按钮上的文字为立即兑换 则点击按钮跳转详情页 buy_btn.click() # 详情页中的购买按钮 targetBtn = wait.until( lambda driver: driver.find_element_by_xpath(btn)) # 详情页中的购买按钮上的文字 targetBtnWord = wait.until( lambda driver: driver.find_element_by_xpath(word)) if targetBtnWord.text != "已抢光": # 如果详情页中购买按钮上的文字不为已枪光 则点击购买 targetBtn.click() time.sleep(5) # 抢到了就退出... break print("抢购结束...")
def get_row(driver: webdriver, d: int) -> list: url = f"https://pvoutput.org/intraday.jsp?id=51500&sid=46834&dt={year}{str(month).zfill(2)}{str(day).zfill(2)}&gs=0&m=0" subtract_day() driver.get(url) driver.implicitly_wait(100) html = driver.page_source soup = BeautifulSoup(html, "html.parser") return soup.find_all("tr", class_=["e", "o"])
def login(driver: webdriver): login_url = "https://pvoutput.org/login.jsp" driver.get(login_url) driver.implicitly_wait(100) username = driver.find_element_by_id("login") password = driver.find_element_by_id("password") username.send_keys(USERNAME) password.send_keys(PASSWORD) driver.find_elements_by_class_name("btn-primary")[0].click()
def get_img(driver: webdriver) -> List[str]: try: driver.implicitly_wait(5) imgs: List[str] = driver.find_elements_by_css_selector( 'div.smallImg>ul>li>a>img') src: List[str] = [] for img in imgs: src.append(img.get_attribute('src').replace('_S', '_L')) return src except Exception as e: print('Can\'t get Images SRC List. Reason %s.' % e)
def get_img(self, driver: webdriver) -> List[str]: try: driver.implicitly_wait(5) imgs: List[str] = driver.find_elements_by_css_selector( 'body > div.page-popup.exhibited-vehicle > div.clfix > div.vehicle-photo-wrap > div.vehicle-thumbnail > ul > li > a > img' ) src: List[str] = [] for img in imgs: src.append(img.get_attribute('src')) return src except Exception as e: print('Can\'t get Images SRC List. Reason %s.' % e)
def _into_room_handle(driver: webdriver, comm: str, current_num: int): driver.implicitly_wait(5) room = driver.find_element_by_css_selector(Order.room_title.value.format(current_num+1)) title = room.text room.click() driver.switch_to.window(driver.window_handles[1]) send_comments(driver, title, comm) driver.close() driver.switch_to.window(driver.window_handles[0])
def get_img_str(driver: webdriver) -> str: try: driver.implicitly_wait(5) imgs: List[str] = driver.find_elements_by_css_selector( 'div.smallImg>ul>li>a>img') img_str: str = '' for img in imgs: src_str = img.get_attribute('src') str_arr = str(src_str).split('/') str_arr.reverse() img_str += str_arr[0].replace( '_S', '_L') + '[:param:][alt=' + driver.find_element_by_css_selector( 'h1.vehicle-Tit' ).text + '][title=' + driver.find_element_by_css_selector( 'h1.vehicle-Tit').text + ']|' return img_str[:-1] except Exception as e: print('Can\'t get Images str. Reason %s' % e)
def get_element(self, driver: webdriver, reference=None, timeout: int = None) -> WebElement: """ return the specified element :param driver: Selenium Driver :param reference: (optional) Identifier of the specific item (where the element is not enough) :param timeout : (options) Number of seconds to wait :return: Selenium Element """ self.reset_error() working_timeout: int = self.get_timeout(timeout) if working_timeout != 0: driver.implicitly_wait(self.get_timeout(timeout)) ret_val = self._get_element(driver, reference) if working_timeout != 0: driver.implicitly_wait(0) return ret_val
def get_img_str(self, driver: webdriver) -> str: try: driver.implicitly_wait(5) imgs: List[str] = driver.find_elements_by_css_selector( 'body > div.page-popup.exhibited-vehicle > div.clfix > div.vehicle-photo-wrap > div.vehicle-thumbnail > ul > li > a > img' ) img_str: str = '' for img in imgs: src_str = img.get_attribute('src') str_arr = str(src_str).split('/') str_arr.reverse() img_str += str_arr[0] + '[:param:][alt=' + self.ko_translate( driver.find_element_by_css_selector( 'body > div.page-popup.exhibited-vehicle > div.vehicle-tit > h2' ).text, "en" ) + '][title=' + self.ko_translate( driver.find_element_by_css_selector( 'body > div.page-popup.exhibited-vehicle > div.vehicle-tit > h2' ).text, "en") + ']|' return img_str[:-1] except Exception as e: print('Can\'t get Images str. Reason %s' % e) return None
def __search_slots(driver: webdriver, spot_preferences: list) -> list: """Searches the booking page for the the preferred timeslots on a given day""" # disable buffer to scan page driver.implicitly_wait(0) # check for slots that match slot preferences and store onClick() reference slots = [] for slot in spot_preferences: found_slot = driver.find_elements_by_xpath(f"//p[./strong[starts-with(text(), '{slot}')]]") if len(found_slot) > 0: logger.info(f"Found the {slot} time slot") wrapper = found_slot[0].find_element_by_xpath("./..") book_now_button = wrapper.find_elements_by_xpath('//button[text()="Book Now"]') # check if book now button was found if len(book_now_button) > 0: slots.append(book_now_button[0].get_attribute("onclick")) else: logger.error("This user already has a reservation for this day!") raise Exception("Slot is already booked for this day!") driver.implicitly_wait(2) return slots
def send_comments(driver: webdriver, room_title: str, comments: str): """ 发送评论功能 """ try: # 等待页面加载完成 element = WebDriverWait(driver, 3).until( EC.presence_of_element_located((By.CSS_SELECTOR, Order.send_button.value)) ) driver.implicitly_wait(5) if element: tag = driver.find_element_by_css_selector(Order.comment.value) tag.send_keys(comments) # 发送成功写入日志 log('normal [在房间 {} 发送了: {}]'.format(room_title, comments)) time.sleep(1) tag.send_keys(Keys.ENTER) driver.find_element_by_css_selector(Order.send_button.value).click() time.sleep(0.5) except Exception as e: log('error ', e) raise ZeroDivisionError("发送评论标签有变导致 selenium 定位出错,请修正")
def activate_mac_filtration(self, browser: webdriver, action: str): """ """ if action == "deny" or action == "allow" or action == "disable": all_wifi_types = browser.find_elements_by_name("w_band") for wifi_type in all_wifi_types: if wifi_type.get_attribute("value") == '2': wifi_type.click() browser.implicitly_wait(3) deny_types = browser.find_elements_by_name( "mac_mode_radio") for deny_type in deny_types: value = deny_type.get_attribute("value") if value == action: deny_type.click() break self.save(browser) browser.implicitly_wait(3) all_wifi_types = browser.find_elements_by_name("w_band") for wifi_type in all_wifi_types: if wifi_type.get_attribute("value") == '5': wifi_type.click() browser.implicitly_wait(3) deny_types = browser.find_elements_by_name( "mac_mode_radio") for deny_type in deny_types: value = deny_type.get_attribute("value") if value == action: deny_type.click() break self.save(browser) browser.implicitly_wait(3) else: raise TypeError( "The entering params must be allow, deny or disable")
def start_session(driver: webdriver) -> None: driver.implicitly_wait(30) driver.maximize_window()
def apply_filters(driver: webdriver, filters_dict: dict, input_soup_element: BeautifulSoup()): for key, val in filters_dict.items(): р3: BeautifulSoup() = None legend: BeautifulSoup() = None span: BeautifulSoup() = None key_soup: BeautifulSoup() = None h3 = input_soup_element.find("h3", string=key) if h3: key_soup = h3.find_parent("div", class_="cBox cBox--content") else: legend = input_soup_element.find("legend", string=key) if legend: key_soup = legend.find_parent("div", class_="g-row u-margin-bottom-9") else: span = input_soup_element.find("span", string=key) if span: key_soup = span.find_parent("div") else: label = input_soup_element.find("label", string=key) if label: key_soup = label.find_parent("div") if isinstance(val, dict): apply_filters(driver, val, key_soup) # checkboxes elif isinstance(val, bool) and val: key_soup = input_soup_element.find(string=key) xpath = xpath_soup(key_soup) label_element = WebDriverWait(driver, DELAY).until(ec.presence_of_element_located((By.XPATH, xpath))) driver.execute_script("arguments[0].click();", label_element) # dropdowns elif isinstance(val, list) and val: variant = 0 # multiselect if ["form-group", "multiselect-dropdown"] == key_soup["class"]: dropdown_element_soup = key_soup.find("span", class_="caret") xpath = xpath_soup(dropdown_element_soup) dropdown_element = WebDriverWait(driver, DELAY).until(ec.element_to_be_clickable((By.XPATH, xpath))) dropdown_element.click() for val_item in val: dropdown_item_soup = key_soup.find("span", class_="text", string=val_item) xpath = xpath_soup(dropdown_item_soup) dropdown_item = WebDriverWait(driver, DELAY).until(ec.element_to_be_clickable((By.XPATH, xpath))) dropdown_item.click() dropdown_element.click() # select and exclude car make and model else: for val_item in val: for val_item_key, val_item_val in val_item.items(): val_item_key_span_soup = key_soup.find_all("span") val_item_key_div_soup = val_item_key_span_soup[variant].find_parent("div") # input text if val_item_key_span_soup[variant].get("class") == ["dropdown-text"]: if val_item_val: input_soup = val_item_key_div_soup.find("input") xpath = xpath_soup(input_soup) input_element = WebDriverWait(driver, DELAY).until( ec.presence_of_element_located((By.XPATH, xpath))) input_element.click() input_element.send_keys(val_item_val) # dropdowns else: if val_item_val: select_soup = val_item_key_div_soup.find("select") xpath = xpath_soup(select_soup) select = Select(WebDriverWait(driver, DELAY).until( ec.presence_of_element_located((By.XPATH, xpath)))) select.select_by_visible_text(val_item_val) driver.implicitly_wait(2) variant += 1 elif (isinstance(val, str) or isinstance(val, int)) and val: if key == "Мощность единицы измерения": pass # sliders elif str(key).endswith(" от") or str(key).endswith(" до"): main_key = key[:-3] side = "left" if str(key).endswith(" до"): side = "right" span = input_soup_element.find("span", string=main_key) div_slider = span.find_parent("div").find("div", class_=f"slider-input u-pull-{side} default-value") input_soup = div_slider.find("input") xpath = xpath_soup(input_soup) input_element = WebDriverWait(driver, DELAY).until(ec.presence_of_element_located((By.XPATH, xpath))) input_element.click() input_element.send_keys(val) else: # single select dropdown select_soup = key_soup.find("select") if select_soup: xpath = xpath_soup(select_soup) select = Select( WebDriverWait(driver, DELAY).until(ec.presence_of_element_located((By.XPATH, xpath)))) select.select_by_visible_text(val) # radiobutton else: radiobutton_soup = key_soup.find("label", string=val) xpath = xpath_soup(radiobutton_soup) radiobutton_element = WebDriverWait(driver, DELAY).until( ec.presence_of_element_located((By.XPATH, xpath))) radiobutton_element.click()