def checkin(info, keepOpen=False, emailBoardingPass=True): url = MAIN_SW_URL + '&confirmationNumber=' + info['confNum'] + \ '&lastName=' + info['lastName'] + '&firstName=' + info['firstName'] try: browser = Chrome(CHROME_DRIVER_PATH) browser.get(url) time.sleep(0.5) # Click the check in button buttonCheckIn = browser.find_element_by_xpath(CHECKIN_BUTTON_XPATH) buttonCheckIn.click() time.sleep(0.5) # Click the email boarding pass button if emailBoardingPass: radioButtonEmail = browser.find_element_by_xpath(EMAIL_RADIO_BUTTON_XPATH) radioButtonEmail.click() time.sleep(0.1) buttonFinal = browser.find_element_by_xpath(FINAL_BUTTON_XPATH) buttonFinal.click() time.sleep(0.5) if not keepOpen: browser.quit() print('successful checkin!') report = messages.successfulCheckIn() status = 1 except: if not keepOpen: browser.quit() report = messages.failedCheckIn() status = -1 return [status, report]
class Twitter: def __init__(self, username, password): self.username = username self.password = password self.driver = Chrome(PATH) def get_driver(self): """ Returns the Driver used for the Scraping """ return self.driver def set_driver(self, driver): if driver == 'chrome': self.driver = Chrome(PATH) elif driver == "edge": print('Sorry, this feature hasnt yet been implemented.') elif driver == "firefox": print('Sorry, this feature hasnt yet been implemented.') else: print('Sorry, I dont know this browser.') def login(self): # navigate to twitter login page using 'get' module self.driver.get('https://twitter.com/login/') sleep(2) # enter the username and password username = self.driver.find_element_by_xpath( '//input[@name="session[username_or_email]"]') username.send_keys(self.username) password = self.driver.find_element_by_xpath( '//input[@name="session[password]"]') # enter the password password.send_keys(self.password) # login using the login button (similar action than to just press enter) password.send_keys(Keys.RETURN) sleep(2) def search(self, searchterm): # enter the searchterm search_input = self.driver.find_element_by_xpath( '//input[@aria-label="Search query"]') search_input.send_keys(searchterm) search_input.send_keys(Keys.RETURN) sleep(2) self.driver.find_element_by_link_text('Latest').click() sleep(1)
def __process(self, name: str, ticker: str): self.__semaphore.acquire() options = webdriver.ChromeOptions() options.add_argument('headless') browser = Chrome(executable_path=self.__driver_path, chrome_options=options) browser.get(self.__url) try: search_form = browser.find_element_by_xpath('''/html/body/div[5]/header/div[1]/div/div[3]/div[1]/input''') search_form.send_keys(name) search_form.send_keys(Keys.ENTER) browser.find_element_by_xpath('''/html/body/div[5]/section/div/div[1]/ul/li[3]/a''').click() news = list() dates = list() # Waiting is necessary, because updated site # marks page as loaded before news table is # ready. So we need to wait until it loads. sleep(self.__delay) table = browser.find_element_by_xpath('''/html/body/div[5]/section/div/div[4]/div[3]/div''') articles = table.find_elements_by_class_name('articleItem') for article in articles: text_div = article.find_element_by_class_name('textDiv') text = text_div.find_element_by_class_name('title').text date = text_div.find_element_by_class_name('articleDetails').find_element_by_class_name('date').text news.append(text) dates.append(date) data = [(replace_dash(change_date(date)), lemmatize(event)) for date, event in zip(dates, news)] with open(self.__store_path + 'News' + ticker + '.csv', 'wb') as file: writer = csv.writer(file, encoding='utf-8') writer.writerow(('Date', 'New')) for row in data: writer.writerow(row) print('Готово: {}'.format(name)) finally: browser.close() self.__semaphore.release()
def library_func(): req = request.get_json() chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument('--no-sandbox') driver = Chrome('/usr/local/bin/chromedriver', options=chrome_options) driver.get('https://oasis.ssu.ac.kr/#/') driver.find_element_by_id('goto-login').click() driver.find_element_by_id('userid').send_keys('20162564') driver.find_element_by_id('password').send_keys('Go970110go!') driver.find_element_by_xpath( "/html/body/div[1]/div[3]/div[2]/div[1]/form/div[3]/button").click() driver.implicitly_wait(3) driver.find_element_by_xpath( '/html/body/div[1]/div[3]/div[2]/div[1]/div[3]/ik-static/ul[1]/li[4]/a' ).click() tbody = driver.find_element_by_xpath( "/html/body/div[1]/div[3]/div[2]/div/div[2]/div[2]/div/div[2]/div/table/tbody" ) rows = tbody.find_elements_by_tag_name('tr') for index, value in enumerate(rows): answer_library.append(value.text) driver.quit() res = { "version": "2.0", "template": { "outputs": [{ "type": "basicCard", "items": [{ "title": "도서관", "description": answer_library[2], "thumbnail": { "imageUrl": "http://k.kakaocdn.net/dn/83BvP/bl20duRC1Q1/lj3JUcmrzC53YIjNDkqbWK/i_6piz1p.jpg" } }] }] } } return jsonify(res)
def news(): global news_json global headlines global news2 driver1= Chrome(executable_path='C:/Users/akash/chromedriver') # News links2 = [] driver1.get("https://economictimes.indiatimes.com/") driver1.find_element_by_xpath( "//a[@href='https://economictimes.indiatimes.com']").click() driver1.find_element_by_xpath("//input[@class='inputBox']").send_keys(company + "\n") driver1.find_element_by_xpath( "//span[@class='mainSprite news']").click() et_news_links = driver1.find_elements_by_xpath( "//div[@class='headerText']//a[contains(@href,'')]") for elem in et_news_links: link = elem.get_attribute("href") links2.append(link) link_news = links2[0] driver1.get(link_news) et_news = [] et = driver1.find_elements_by_xpath( "//div[@class='eachStory']//a[contains(@href,'')]") for elem in et: link = elem.get_attribute("href") et_news.append(link) et_news = [string for string in et_news if string.endswith(".cms")] news_links = et_news[:15] headlines = [] news2 = [] for link in news_links: # annual report driver1.get(link) headline = driver1.find_element_by_xpath( '//h1[@class="clearfix title"]').text headlines.append(headline.replace("\n", "")) article = driver1.find_element_by_xpath( '//div[@class="Normal"]').text news2.append(article.replace("\n", "")) news_format = [] for headline, link in zip(headlines, news_links): news_dict = {'title': headline, 'link': link} news_format.append(news_dict) news_json = json.dumps(news_format) return news_json
def pingjia(driver: Chrome) -> None: # 限制不能给满分,第一个选项四星 driver.find_element_by_xpath('//div[@class="controls" and label[@class="radio"]]/label[2]').click() # 其他选项全部五星 labels = driver.find_elements_by_xpath('//div[@class="controls" and label[@class="radio"]]/label[1]')[1:] for label in labels: label.click() # 意见填无 textarea = driver.find_element_by_xpath('//*[@id="pjnr"]/li[7]/fieldset/ol/li/div[3]/div/textarea') textarea.send_keys('无') submit_button = driver.find_element_by_xpath('//*[@id="pjsubmit"]') submit_button.click() # 评教成功后关闭弹窗 close_button = driver.find_element_by_xpath('//*[@id="finishDlg"]/div[2]/button') close_button.click()
def test_firstcase(): path="C:\\Users\\Nitin Tyagi\\Downloads\\chromedriver_win32\\chromedriver.exe" driver = Chrome(executable_path=path) driver.get("http://www.theTestingWorld.com/testings") # Maximize browser driver.maximize_window() # Enter Data to the textbox driver.find_element_by_name("fld_username").send_keys("helloworld") driver.find_element_by_xpath("//input[@name='fld_email']").send_keys("*****@*****.**") driver.find_element_by_name("fld_password").send_keys("abcd123") driver.find_element_by_name("fld_cpassword").send_keys("abcd123") driver.find_element_by_name("fld_username").clear() driver.find_element_by_name("fld_username").send_keys("abcd123")
def check_element_exist(driver: webdriver.Chrome, path: str, by: str = 'xpath') -> bool: try: if by == 'xpath': driver.find_element_by_xpath(path) elif by == 'css': driver.find_element_by_css_selector(path) elif by == 'id': driver.find_element(By.ID, path) else: logger.info(f"Unable to check element using '{by}'") except NoSuchElementException: return False return True
def main(): template = 'This translation is culturally inappropriate. The correct translation should be %s. I selected Traditional Chinese, which should translate into phrases used in Taiwan, but it gave me phrases used in China (Simplified Chinese).' url = 'https://translate.google.com/#view=home&op=translate&sl=auto&tl=zh-TW&text=%s' data = get_sheet() if os.path.exists('./chromedriver.exe'): driver = Chrome() else: driver = Firefox() driver.maximize_window() driver.implicitly_wait(1) first = True try: for i, d in enumerate(data): print('%-50s' % f'{i + 1}/{len(data)} {d[0]} => {d[1]}', end='\r') if d[3].lower() == 'ok': continue try: driver.get(url % d[0]) if first: driver.find_element_by_class_name('tlid-dismiss-button').click() element = driver.find_element_by_class_name('tlid-send-feedback-link') driver.execute_script("arguments[0].click();", element) time.sleep(1) n = len(driver.find_elements_by_tag_name('iframe')) for i in reversed(range(n)): driver.switch_to.frame(i) e = driver.find_elements_by_tag_name('textarea') if len(e) == 1: e[0].send_keys(template % d[1]) driver.find_element_by_xpath('//input[@type="checkbox"]').click() driver.find_element_by_xpath('//button[@type="submit"]').click() driver.switch_to.default_content() break driver.switch_to.default_content() time.sleep(1) driver.find_element_by_class_name('tlid-suggest-edit-button').click() driver.find_element_by_class_name('contribute-target').clear() driver.find_element_by_class_name('contribute-target').send_keys(d[1]) driver.find_element_by_class_name('jfk-button-action').click() first = False except Exception as e: print(f'\nError, skip!') finally: driver.quit() print('Done!')
def get_title_by_asin(asin): """ Scraping function using Selenium for getting product names by Amazon ASIN. Args: asin: Amazon ASIN unique product ID Returns: title: title of product on Amazon with that ASIN """ # Create search result page from ASIN link = "https://www.amazon.com/s?k=" + asin + "&ref=nb_sb_noss" # Instatiate browser browser = Chrome() browser.get(link) # Find title by unique XPath to first result (most common format) try: title = browser.find_element_by_xpath( '//*[@id="search"]/div[1]/div[2]/div/span[3]/div[1]\ /div/div/div/div/div[2]/div[2]/div/div[1]/div/div/div\ [1]/h2/a/span').text except Exception: title = "None found" browser.close() return title
def date_set(self, url): try: date = Chrome("./PC/chromedriver.exe") date.get(url) time.sleep(5) button = date.find_element_by_xpath("//*[@id='foreignTable']/div") button.click() html = date.page_source soup = BeautifulSoup(html, "lxml") sum_1 = soup.select(".VirusTable_1-1-300_2AH4U9") title = soup.select(".VirusTable_1-1-300_26gN5Z")[1] self.title = [i.text for i in title.select("th div")] for i in sum_1: name = i.select("td a div")[0].text self.name_ls.append(name) sum_2 = i.select("td")[1:] ls = { self.title[0]: name, self.title[1]: sum_2[0].text, self.title[2]: sum_2[1].text, self.title[3]: sum_2[2].text, self.title[4]: sum_2[3].text } self.ls_text.append(ls) self.save("./Data/data_all.csv", "csv", self.ls_text) date.quit() return True except Exception as E: print(f"【ERROR】(data_all): {E}") return False
def test_mouse(): #for time.sleep import time path = "D:\\chromedriver_win32\\chromedriver.exe" driver = Chrome(executable_path=path) driver.get("http://www.theTestingWorld.com/") obj = ActionChains(driver) #click any where where ever the control is #obj.click().perform() #to click on any specific element #obj.click(driver.find_element_by_xpath("//a[text()='Login']")).perform() #right click #obj.context_click().perform() #obj.context_click(driver.find_element_by_xpath("//a[text()='Login']")).perform() #double click #obj.double_click().perform() #obj.double_click(driver.find_element_by_xpath("//a[text()='Login']")).perform() # obj.move_to_element( driver.find_element_by_xpath( "//span[contains(text(),'TUTORIAL')]")).perform()
def solve_anti_automation_challenge(driver: webdriver.Chrome): try: element = driver.find_element_by_xpath( anti_automation_challenge_captcha_xpath) captcha_url = element.get_attribute("src") display_image(captcha_url) captcha = str(input("Anti-Automation Challenge Captcha is: ")) if captcha: driver.find_element_by_xpath( anti_automation_challenge_input_xpath).send_keys(captcha) driver.find_element_by_xpath( anti_automation_challenge_submit_xpath).click() else: raise NoSuchElementException except NoSuchElementException: logger.error("Not able to solve anti-automation challenge page")
def solve_authentication_required(driver: webdriver.Chrome): try: if get_authentication_required_type( driver) is AuthenticationRequiredType.OTP: otg = str(input("OTG is: ")) if otg: driver.find_element_by_xpath( anti_automation_challenge_input_xpath).send_keys(otg) driver.find_element_by_xpath( anti_automation_challenge_submit_xpath).click() else: raise NoSuchElementException element = driver.find_element_by_id(continue_element_id) element.click() except NoSuchElementException: logger.error("Not able to solve Authentication required page")
class NewRoomTestCase(StaticLiveServerTestCase): def setUp(self): self.browser = Chrome() self.browser.implicitly_wait(10) user = User.objects.create_user(username='******', name="Administrador Teste", email='*****@*****.**', password='******', is_staff=True, is_superuser=True) coordinator = Coordinator.objects.create(usp_number='1234567', user=user) teacher = Teacher.objects.create(usp_number='1234567', user=user) self.browser.get('%s%s' % (self.live_server_url, reverse_lazy("login"))) self.browser.find_element_by_id('id_username').send_keys('*****@*****.**') self.browser.find_element_by_id('id_password').send_keys('1234567') self.browser.find_element_by_id('login').click() def tearDown(self): self.browser.quit() def test_room_sign_up_fire(self): self.browser.find_element_by_id('coordinator-rooms').click() self.browser.find_element_by_id('new-room').click() modality_select = Select(self.browser.find_element_by_id("id_block")) modality_select.select_by_visible_text("Bloco A") modality_select = Select(self.browser.find_element_by_id("id_floor")) modality_select.select_by_visible_text("Térreo") self.browser.find_element_by_id('id_identifier').send_keys('ST') self.browser.find_element_by_id('submit').click() self.assertIn(('%s%s' % (self.live_server_url, reverse_lazy("rooms_list"))), self.browser.current_url) self.assertTrue(self.browser.find_element_by_xpath("//div[@class='card']/div[@class='card-body elegant-color white-text rounded-bottom']/a[@class='white-text'][contains(.,'AT-ST')]"))
class TestSearchPom(unittest.TestCase): def setUp(self) -> None: opts = Options() opts.headless = False self.driver = Chrome('./drivers/chromedriver.exe', options=opts) self.driver.get('https://www.google.com/') self.search_page = SearchPage(self.driver) def test_search_pom_instance(self): self.assertNotEqual(self.search_page, None) def test_validate_driver(self): self.assertNotEqual(self.search_page.driver, None) def test_input_method(self): self.search_page.input('google') input_xpath = '//*[@id="tsf"]/div[2]/div[1]/div[2]/div[2]/ul/li[' \ '1]/div/div[2]/div/span' input_element = self.driver.find_element_by_xpath(input_xpath) self.assertEqual(input_element.text, 'google') def test_click_search(self): self.search_page.input('google') self.search_page.click_search() self.assertRaises(NoSuchElementException, self.driver.find_element_by_xpath, self.search_page.button_xpath) def tearDown(self) -> None: self.driver.close()
def login(): url = "http://newxk.urp.seu.edu.cn/xsxkapp/sys/xsxkapp/*default/index.do#cvUnProgramCourse" chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('disable-infobars') driver = Chrome(executable_path='chromedriver', options=chrome_options) driver.get(url) img_src = driver.find_element_by_xpath( '//*[@id="vcodeImg"]').get_attribute("src") print(img_src) image = requests.get(img_src) # 百度 # res = client.basicAccurate(image.content) # print(res) # if not res: # print("验证码识别出错") # print(res) # return False # verify = res['words_result'][0]['words'] verify, req_id = api.Predict(pred_type, image.content) print(f"img_src:{img_src}") verify = verify.strip(" ").strip() token = img_src[len( "http://newxk.urp.seu.edu.cn/xsxkapp/sys/xsxkapp/student/vcode/image.do?vtoken=" ):] # 使用验证码结果请求得到token driver.quit() return verify, token
def get_menu_data(driver: webdriver.Chrome, categories: List[Text]) -> Dict[Text, Any]: """Scrape data for each menu item and return a dict of items. Args: driver (Any): Chrome driver categories (List[Text]): a list of urls to menu category pages Returns: Dict[Text, Any]: item name, item info dict pairs """ wait = WebDriverWait(driver, WAIT_TIME) menu_dict = {} # loop through each category for c in categories: try: driver.get(c) xpath = '//a[@class="block linkOverlay__primary prodTile"]' wait.until(EC.visibility_of_element_located((By.XPATH, xpath))) # grab the links to each item in the category item_links = [link.get_attribute('href') for link in driver.find_elements_by_xpath(xpath)] cat_name_xpath = '//h1[@class="sb-heading pb4 md-pb5 lg-pb7 text-bold"]' cat_name = driver.find_element_by_xpath(cat_name_xpath).text # send each link to get item data and get the returned dict key/value for item in item_links: scrape_result = get_item_data(driver, item, cat_name) if scrape_result: item_name, item_data = scrape_result menu_dict[item_name] = item_data except Exception as e: logger.error(e) return menu_dict
def t4_verify_drag_drop(self): ''' function to verify the 'Drag and Drop' :return: Returns boolean value ''' try: browser = Chrome(self.chrome_path) ins_obj = inspect.currentframe() func_name = inspect.getframeinfo(ins_obj).function print(func_name) ss_file = RESULT + "\\" + func_name + "_" + str( time.time()).split('.')[0] + ".png" browser.get(self.home_page) browser.find_element_by_link_text("Drag and Drop").click() source1 = browser.find_element_by_xpath('//*[@id ="column-a"]') target1 = browser.find_element_by_xpath('//*[@id ="column-b"]') action = ActionChains(browser) action.drag_and_drop(source1, target1).perform() self.drag_and_drop(browser, source1, target1) browser.save_screenshot(ss_file) self.logger.info("Drag and Drop verified successfully") browser.close() return True except Exception as err: self.logger.error(err) return False
def create_object(link: str): driver = Chrome('selenium/chromedriver.exe') driver.get(link) try: # Get script with object info script_text = '' scripts = driver.find_elements_by_xpath( "//script[@type='text/javascript']") for script in scripts: if script.get_attribute('innerHTML')[0:15] == ';FE.manageEvent': script_text = script.get_attribute('innerHTML') # Fill object object = Object() object.id = int(re.findall('"objectId":[0-9]+', script_text)[0][11:]) object.type = driver.find_element_by_xpath( '//div[@class="col_3c"]/div').text object.address = re.findall('"address": "[^"]+"', script_text)[0][12:-1] object.lat = re.findall('"objectLat": [0-9.]+,', script_text)[0][12:-1] object.lon = re.findall('"objectLon": [0-9.]+,', script_text)[0][12:-1] # Push object to DB session = new_session() object.push(session) session.commit() session.close() except Exception: log_error(type='create_object', object_link='link') pass driver.close()
def google_login(driver: webdriver.Chrome, google_mail, google_password): """Login to google first to make Captchas easier""" # from https://stackoverflow.com/questions/60117232/selenium-google-login-block/60342877#60342877 driver.get( "https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent%27" ) wait_set_time_plus_random_time(3) driver.find_element_by_xpath('//*[@id="openid-buttons"]/button[1]').click() driver.find_element_by_xpath('//input[@type="email"]').send_keys( google_mail) driver.find_element_by_xpath('//*[@id="identifierNext"]').click() wait_set_time_plus_random_time(3) driver.find_element_by_xpath('//input[@type="password"]').send_keys( google_password) driver.find_element_by_xpath('//*[@id="passwordNext"]').click() wait_set_time_plus_random_time(2)
def get_page_num(api: Chrome): try: result = api.find_element_by_xpath("//div[@id='result-stats']") tag_num = result.text.strip().split(' ')[1] print(tag_num) except NoSuchElementException: return None
def get_acc_balance(chrome: Chrome): try: xpath = '//*[@id="box_conteudo"]/table[1]/tbody/tr[1]/td[2]' log_client.set_msg( log_type="info", log_msg=f"trying to reach element at xpath: {xpath}") acc_elem = chrome.find_element_by_xpath(xpath=xpath) sleep(2) log_client.set_msg(log_type="info", log_msg="element was reached successfully") log_client.set_msg(log_type="info", log_msg="parsing element text properties") acc_balance_str = acc_elem.text.replace(".", "").replace(",", ".") return acc_balance_str except Exception as e: log_client.set_msg( log_type="error", log_msg=f"the following error occurred with args: {e.args}") finally: chrome.quit()
def get_baidu_hot(): """ :return: 返回百度疫情热搜 """ option = ChromeOptions() # 创建谷歌浏览器实例 option.add_argument("--headless") # 隐藏浏览器 option.add_argument('--no-sandbox') url = "https://voice.baidu.com/act/virussearch/virussearch?from=osari_map&tab=0&infomore=1" browser = Chrome( options=option, executable_path= ".C:\\Program Files (x86)\\Google\Chrome\\Application/chromedriver.exe" ) browser.get(url) # 找到展开按钮 dl = browser.find_element_by_xpath( '//*[@id="main"]/div/div/section/div[2]/div/div[2]/section/div') dl.click() time.sleep(1) # 找到热搜标签 c = browser.find_elements_by_xpath( '//*[@id="main"]/div/div/section/div[2]/div/div[2]/section/a/div/span[2]' ) context = [i.text for i in c] # 获取标签内容 print(context) return context
def process_request(self, request, spider): # Called for each request that goes through the downloader # middleware. browser = Chrome() browser.get('https://so.gushiwen.cn/user/login.aspx') browser.execute_script("document.body.style.zoom='0.8'" ) # win10系统显示设置默认缩放125%,因此为了截图需要将浏览器缩放80%来复原 browser.maximize_window() browser.get_screenshot_as_file('code.png') code = browser.find_element_by_xpath('//img[@id="imgCode"]') left = int(code.location['x']) top = int(code.location['y']) right = left + int(code.size['width']) bottom = top + int(code.size['height']) im = Image.open('code.png') im = im.crop((left, top, right, bottom)) im.save('code1.png') code_str = rec_code('code1.png') login(browser, code_str) listcookies = browser.get_cookies() print(listcookies) browser.close() request.cookies = listcookies # 设置请求的cookies信息 # request.cookies = get_cookie() # Must either: # - return None: continue processing this request # - or return a Response object # - or return a Request object # - or raise IgnoreRequest: process_exception() methods of # installed downloader middleware will be called return None
def test_runtime(): #chromedriver link path = "D:\\chromedriver_win32\\chromedriver.exe" #object of browser object driver = Chrome(executable_path=path) #passing url driver.get("http://www.theTestingWorld.com/testings") #max browser driver.maximize_window() #fetch the title #print(driver.title) #fetch url #print(driver.current_url) #fetch page source #print("-**") #print(driver.page_source) #fetching element text print("text on link is " + driver.find_element_by_class_name("displayPopup").text) #fetching attribute print("value of button " + driver.find_element_by_xpath( "//input[@type='submit']").get_attribute("value"))
def update_province(self): option = ChromeOptions() option.add_argument("headless") chrome = Chrome(options=option) chrome.get("https://news.qq.com/zt2020/page/feiyan.htm#/?nojump=1") update_time = chrome.find_element_by_xpath( "/html/body/div[1]/div[2]/div[3]/div[1]/div[2]/p/span") province = chrome.find_elements_by_xpath( "/html/body/div[1]/div[2]/div[4]/div[3]/table[2]/tbody/tr[1]/th/p[1]/span" ) nowConfirm = chrome.find_elements_by_xpath( "/html/body/div[1]/div[2]/div[4]/div[3]/table[2]/tbody/tr[1]/td[1]/p[1]" ) confirm = chrome.find_elements_by_xpath( "/html/body/div[1]/div[2]/div[4]/div[3]/table[2]/tbody/tr[1]/td[2]/p[1]" ) if not Province.query.filter_by(update_time=update_time.text).first(): for i in range(len(province)): db.session.add( Province(update_time=update_time.text, province=province[i].text, nowConfirm=int(nowConfirm[i].text), confirm=int(confirm[i].text))) db.session.commit() ret = Province.query.filter( Province.update_time != update_time.text).all() for ri in ret: db.session.delete(Province.query.get(ri.id)) db.session.commit()
def save_screenshot_byXpath(driver: webdriver.Chrome, xpath: str, path: str) -> None: # Ref: https://stackoverflow.com/a/52572919/ if os.path.exists(path): os.remove(path) screenshotBytes = driver.find_element_by_xpath(xpath).screenshot_as_png screenshot = Image.open(BytesIO(screenshotBytes)) screenshot.save(path)
def test_sign_in_code(browser: webdriver.Chrome): browser.get("https://eda.yandex/") time.sleep(2) browser.find_element_by_xpath( "//*[@id=\"root\"]/div/header/div/button" ).click() # click sign in button time.sleep(2) code_field = browser.find_element_by_xpath( "/html/body/div[3]/div/div/div[3]" "/div/div[2]/div[4]/input" ) code_field.send_keys("1223") assert code_field.get_attribute("value") == "1223" code_field.clear() code_field.send_keys("122345") assert code_field.get_attribute("value") == "1223"
class Browser: def __init__(self): self.headless = False options = Options() options.headless = self.headless # To make browser visible set it to False path = install() # Path of the chromedriver.exe self.driver = Chrome(executable_path=path, options=options) def join_btn_clicker(self): join_btn = self.driver.find_element_by_xpath("""//*[@id="joinBtn"]""") join_btn.click() def meeting_id_in(self): id_join = True while id_join: try: self.join_btn_clicker() id_join = False except NoSuchElementException: pass sleep(2) def meeting_password_in(self): password_box = self.driver.find_element_by_xpath( '''//*[@type="password"]''') password_box.send_keys(self.meeting_password) pass_join = True while pass_join: try: self.join_btn_clicker() pass_join = False except NoSuchElementException: pass def join_via_pass_id(self): self.meeting_id = input("Enter Meeting ID: ") self.meeting_password = input("Enter Meeting Password: "******"https://zoom.us/wc/join/" + self.meeting_id) self.meeting_id_in() self.meeting_password_in() print("Done!") while True: pass def join_via_link(self, link_src): self.driver.get(link_src) self.join_btn_clicker()
def test_apple_cart(): driver = Chrome("//Users/Dakoda/python/_drivers/chromedriver") driver.get("https://apple.com") driver.find_element_by_xpath( '//*[@id="ac-globalnav"]/div/ul[2]/li[2]/a').click() driver.find_element_by_xpath( '//*[@id="chapternav"]/div/ul/li[3]/a/figure').click() driver.find_element_by_class_name('ac-ln-button').click() driver.find_elements_by_xpath( '//*[@class="button button-block"]')[2].click() driver.find_element_by_css_selector('button[name="add-to-cart"]').click() driver.find_element_by_css_selector( 'button[class="merchandising button"]').click() driver.find_element_by_css_selector( 'button[id="shoppingCart.actions.checkout"]').click() element = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.ID, "guest-checkout"))) element.click() element = WebDriverWait(driver, 20).until( EC.element_to_be_clickable( (By.CSS_SELECTOR, 'button[data-autom="continue-button-label"]'))) element.click() element = WebDriverWait(driver, 20).until( EC.element_to_be_clickable(( By.CSS_SELECTOR, 'input[id="checkout.shipping.addressSelector.newAddress.address.firstName' ))) element.send_keys('Dakoda') driver.find_element_by_css_selector( 'input[id="checkout.shipping.addressSelector.newAddress.address.lastName' ).send_keys('Nielsen') driver.find_element_by_css_selector( 'input[id="checkout.shipping.addressSelector.newAddress.address.street' ).send_keys('312 S Main St') driver.find_element_by_css_selector( 'input[id="checkout.shipping.addressSelector.newAddress.address.zipLookup.postalCode' ).send_keys('84104') driver.find_element_by_css_selector( 'input[id="checkout.shipping.addressContactEmail.address.emailAddress' ).send_keys('*****@*****.**') driver.find_element_by_css_selector( 'input[id="checkout.shipping.addressContactPhone.address.fullDaytimePhone' ).send_keys('8011231234') driver.find_element_by_css_selector( 'button[data-autom="continue-button-label"]').click() assert driver.title == "Shipping Details — Secure Checkout"
url = 'http://staging.totsy.com/' def Input_Field(Field_name,Field_value): Browser.find_element_by_xpath(Field_name).send_keys(Field_value) def Link(Link_Name): Browser.find_element_by_xpath(Link_Name).click() Browser.get(url) #Log-in Credential Input_Field("//*[@id='email']","*****@*****.**") Input_Field("//*[@id='pass']",'skashem3422') Link("//*[@id='submit-button']") #Mouse hover to SHOP BY CATEGORY shop_by_category = Browser.find_element_by_xpath("//*[@id='navByCat']/a/em") hover = ActionChains(Browser).move_to_element(shop_by_category) hover.perform() #Click on boys apparel Link("//*[@id='navByCat']/ul/li[2]/a") Link("//*[@id='mainContent']/div[2]/div/div[1]/ul/li[1]/div/a/img") Link("//*[@id='attribute169']") #select size Link("/html/body/div/div/section/div/div[2]/div[3]/form/div[2]/div[2]/fieldset/div/div[2]/div/select/option[2]") #add to cart Link("/html/body/div/div/section/div/div[2]/div[3]/form/div[2]/div[2]/div/div/button") #checkout
def start_callback(): """ Main loop of the scrape. """ profile_username = E_username.get() # The Instagram username of the profile from which we # are downloading. Must be supplied. output_directory = E_path.get() # Will be initialized with the optional argument or a # default later. update_mode = True serialize = True latest_image = '' # The latest downloaded images will be the first in the directory. files = os.listdir(output_directory) if files: latest_image = files[0] # Start the browser driver = Chrome(executable_path='../bin/chromedriver') driver.get(insta_url + profile_username) # Find the number of posts on this Instagram profile post_count_tag_xpath = ('//*[@id="react-root"]/section/main/' + 'article/header/div[2]/ul/li[1]/span/span') post_count_tag = driver.find_element_by_xpath(post_count_tag_xpath) post_count = int(post_count_tag.text.replace(',', '')) # If the target profile is private, then redirect to the login page login_tag_xpath = '//*[@id="react-root"]/section/main/article/div/p/a' try: login_tag = driver.find_element_by_xpath(login_tag_xpath) login_page_url = login_tag.get_attribute('href') driver.get(login_page_url) # Wait for the user to login while driver.current_url == login_page_url: sleep(1) # Return to the target profile from the homepage driver.get(insta_url + profile_username) except: pass # Click the 'Load More' element driver.find_element_by_class_name('_oidfu').click() # Load all the posts into the browser processed = 0 while processed < post_count: # Load more content by scrolling to the bottom of the page driver.execute_script("window.scrollTo(0, 0);") driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Download 4 rows of items (4 rows are loaded upon each scroll) and # remove them from view for _ in itertools.repeat(None, 4): urls = fetch_row_links(driver) delete_row(driver) for url in urls: # Exit if we've reached the latest image that was in the # directory before downloading. This means the directory has # everything beyond this point. if update_mode: fname = file_name.search(url).group(0) if fname in latest_image: exit(0) download_from_url(url, output_directory, serialize, post_count-processed) processed += 1 driver.close()