def get_links_from_driver(driver: WebDriver) -> List[str]: elems = driver.find_elements_by_xpath("//a[@href]") # Get politics links links = [] for elem in elems: href = elem.get_attribute("href") if href.startswith('https://m.news24.com/'): # Handle links from the mobile site href = 'https://www' + href[9:] if href.startswith('https://www.news24.com/SouthAfrica/News/'): links.append(href) return links
def cleanup_cart(browser: webdriver.WebDriver, task_data: TaskData): open_cart_page(browser) product_row_xpath = "//div[contains(@class, 'newcart_box')]" \ "//div[contains(@class, 'newcart_main')]" \ "//ul[contains(@class, 'newcart_list_items')]" product_link_xpath = ".//li[contains(@class, 'newcart_product')]//a[contains(@class, 'title')]" products_in_cart = list( map(lambda p: p.find_element_by_xpath(product_link_xpath).text, browser.find_elements_by_xpath(product_row_xpath))) logging.info( "Finding products added from the task in the list of products in cart") while task_data.products and set( task_data.products).intersection(products_in_cart): logging.info( "List of added products and list of products in cart intersect.") for product in task_data.products: logging.info(f"Working with product {product}.") try: _remove_one_product_from_cart(browser, task_data, product_row_xpath, product) logging.info(f"Done with product {product}. Moving on...") break except NoSuchElementException: logging.error( f"Product {product} was not found in cart and was probably already deleted. Moving on..." ) logging.error(traceback.format_exc()) task_data.products.remove(product) break browser.refresh() products_in_cart = list( map(lambda p: p.find_element_by_xpath(product_link_xpath).text, browser.find_elements_by_xpath(product_row_xpath)))
def fetch_dogs_list(driver: WebDriverClass) -> list: """Fetch list of available rescue dogs. Refreshes the webdriver instance and parses the pages for available rescue dogs. For now, it's implemented by first matching a <div> of class "dogs col-md-12" (hope it doesn't change) and then matching all the children span elements. Parameters ---------- driver : WebDriverClass selenium webdriver instance (already initialized) Returns ------- list List of elements matching the spans containing dogs """ driver.refresh() time.sleep(2) dog_list = driver.find_elements_by_xpath( "//div[@class='dogs col-md-12']/span") dog_list = [dog.text for dog in dog_list] return dog_list
wd.implicitly_wait(wait) wd.get( "https://acorn.utoronto.ca/sws/welcome.do?welcome.dispatch#/") if wd.find_elements_by_id("inputID"): wd.find_element_by_id("inputID").click() wd.find_element_by_id("inputID").clear() wd.find_element_by_id("inputID").send_keys(utorid) wd.find_element_by_id("inputPassword").click() wd.find_element_by_id("inputPassword").clear() wd.find_element_by_id("inputPassword").send_keys(password) wd.find_element_by_name("login").click() wd.find_element_by_link_text("Manage Courses").click() wd.find_element_by_link_text(program_year).click() wd.find_element_by_id("searchBox").click() wd.find_element_by_id("searchBox").clear() wd.find_element_by_id("searchBox").send_keys(course) wd.find_element_by_id("searchBox").click() WebDriverWait(wd, 10).until( EC.presence_of_element_located( (By.ID, "course_search_results_list"))) wd.find_elements_by_xpath( "//ul[@id='course_search_results_list']//li//a")[term].click() if wd.find_elements_by_link_text("Enrol"): wd.find_element_by_link_text("Enrol").click() courses.remove(course) print("%s was successfully enrolled. Congrats!\n" % course) break except Exception: continue wd.quit()
class ExchangeCreditTest(StaticLiveServerTestCase): def setUp(self): self.selenium = WebDriver() self.user = User.objects.create_user(username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234432112344321", notification_preference="S") self.selenium.implicitly_wait(10) self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/exchange/')) def tearDown(self): self.selenium.quit() def __get_page_1(self): class ExchangePage(object): def __init__(self, selenium): self.selenium = selenium self.from_curr = Select( self.selenium.find_element_by_id('id_currency')) self.to_curr = Select( self.selenium.find_element_by_id('id_final_currency')) self.amount = self.selenium.find_element_by_id( 'id_final_amount') self.button = self.selenium.find_element_by_xpath( "//button[@type='submit']") return ExchangePage(self.selenium) def __get_page_2(self): class ExchangePreviewPage(object): def __init__(self, selenium): self.selenium = selenium self.confirm_exchange = self.selenium.find_element_by_xpath( "//button[@type='submit']") return ExchangePreviewPage(self.selenium) @staticmethod def _fill_1(page): page.amount.send_keys('1') page.from_curr.select_by_visible_text('﷼') page.to_curr.select_by_visible_text('$') def login(self): user = User.objects.get(username="******") SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore() session[SESSION_KEY] = User.objects.get(username="******").id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = user.get_session_auth_hash() session.save() cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', } self.selenium.add_cookie(cookie) self.selenium.refresh() self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/exchange/')) @staticmethod def __get_text(element): return element.get_attribute('textContent') def charge_wallet(self, amount): self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/charge/')) self.selenium.find_element_by_id('id_amount').send_keys(amount) self.selenium.find_element_by_id('charge-button').click() self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/exchange/')) def test_preview_exchange_not_empty(self): self.login() page = self.__get_page_1() self._fill_1(page) page.amount.clear() page.button.click() success_set = self.selenium.find_elements_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertEqual(len(success_set), 0) page.amount.send_keys('1') page.from_curr.select_by_visible_text('---------') page.button.click() success_set = self.selenium.find_elements_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertEqual(len(success_set), 0) page.from_curr.select_by_visible_text('﷼') page.to_curr.select_by_visible_text('---------') page.button.click() success_set = self.selenium.find_elements_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertEqual(len(success_set), 0) page.to_curr.select_by_visible_text('$') def test_preview_exchange(self): self.login() self.charge_wallet(550000) page = self.__get_page_1() self._fill_1(page) page.button.click() self.selenium.implicitly_wait(10) self.assertIn("accept", self.selenium.current_url) def test_preview_accept(self): self.login() self.charge_wallet(550000) page = self.__get_page_1() self._fill_1(page) page.button.click() self.selenium.implicitly_wait(10) page2 = self.__get_page_2() page2.confirm_exchange.click() time.sleep(3) credit = self.selenium.find_element_by_id("wallet_balance_$") self.assertEqual(float(1), float(self.__get_text(credit))) self.assertNotIn("accept", self.selenium.current_url)
class EditTest(StaticLiveServerTestCase): fixtures = ['user_panel_data.json'] def setUp(self): self.selenium = WebDriver() self.user = User.objects.create_user(username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234432112344321", notification_preference="S") self.selenium.implicitly_wait(10) self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/profile/')) def tearDown(self): self.selenium.quit() def login(self): user = User.objects.get(username="******") SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore() session[SESSION_KEY] = User.objects.get(username="******").id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = user.get_session_auth_hash() session.save() cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', } self.selenium.add_cookie(cookie) self.selenium.refresh() self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/profile/')) def __get_page(self): class EditPage(object): def __init__(self, selenium): self.selenium = selenium self.username = self.selenium.find_element_by_id('id_username') self.email = self.selenium.find_element_by_id('id_email') self.fname = self.selenium.find_element_by_id('id_first_name') self.lname = self.selenium.find_element_by_id('id_last_name') self.phone = self.selenium.find_element_by_id( 'id_phone_number') self.notification = Select( self.selenium.find_element_by_id( 'id_notification_preference')) self.button = self.selenium.find_element_by_xpath( "//button[@type='submit']") return EditPage(self.selenium) @staticmethod def _fill(page): page.username.clear() page.email.clear() page.fname.clear() page.lname.clear() page.phone.clear() page.username.send_keys('parand') page.email.send_keys('*****@*****.**') page.fname.send_keys('Parand') page.lname.send_keys('Alizadeh') page.phone.send_keys('02347898557') page.notification.select_by_visible_text('sms') @staticmethod def __get_text(element): return element.get_attribute('textContent') @staticmethod def __send_keys_scrolling(input_element, keys): _ = input_element.location_once_scrolled_into_view time.sleep(1) input_element.send_keys(keys) def test_empty_parts_edit(self): self.login() page = self.__get_page() username = page.username username.clear() button = page.button button.click() success_set = self.selenium.find_elements_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertEqual(len(success_set), 0) def test_successful_edit(self): self.login() page = self.__get_page() self._fill(page) page.button.click() success = self.selenium.find_element_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertIn("Profile successfully updated!", self.__get_text(success))