def find_element_by_locator(self, locator): locator_type, locator_value = locator.split('=') if locator_type == 'id': return self.find_element_by_id(locator_value) elif locator_type == 'class': e = WebElement(self.find_element_by_class_name(locator_value)) elif locator_type == 'tag': e = WebElement(self.find_element_by_tag_name(locator_value)) elif locator_type == 'css': e = WebElement(self.find_element_by_css_selector(locator_value)) elif locator_type == 'xpath': e = WebElement(self.find_element_by_xpath(locator_value)) else: raise InvalidLocatorString(locator) return e
def refine_search_with(self, ref, opt): refine_by = ref.lower() if "price" in refine_by and isinstance(opt, list): el = self.driver.find_element_by_locator( locators['search_price_low']) el.clear() el.send_keys(price_range[0]) el = self.driver.find_element_by_locator( locators['search_price_high']) el.clear() el.send_keys(price_range[1]) self.driver.find_element_by_locator( locators['search_price_go']).click() else: option = opt.lower() ref_dict = self.get_refine_search_dict() if refine_by in ref_dict.keys() and option in ref_dict[refine_by]: el = refine_search_dict[refine_by][option] if isinstance(el, WebElement): WebElement(el).click() else: self.driver.find_element_by_locator(el).click() else: raise Exception( 'No such option or refine by parameter in the dict') self.wait_for_available(locators['left_nav_section'])
def find_element_by_locator(self, locator): locator_type = locator[:locator.find('=')] if locator_type == '': raise framework.exceptions.InvalidLocatorString(locator) locator_value = locator[locator.find('=') + 1:] if locator_type == 'id': return self.find_element_by_id(locator_value) elif locator_type == 'class': e = WebElement(self.find_element_by_class_name(locator_value)) elif locator_type == 'tag': e = WebElement(self.find_element_by_tag_name(locator_value)) elif locator_type == 'css': e = WebElement(self.find_element_by_css_selector(locator_value)) elif locator_type == 'xpath': e = WebElement(self.find_element_by_xpath(locator_value)) else: raise InvalidLocatorString(locator) return e
def get_results(self): titles = [] prices = [] ratings = [] els = self.driver.find_elements_by_locator(locators["results_list"]) for el in els: title = WebElement(el).find_element_by_locator( locators['results_title']).text[:16] try: p = WebElement(el).find_element_by_locator( locators['results_price']).get_attribute("innerHTML") r = WebElement(el).find_element_by_locator( locators['results_rating']).get_attribute("innerText") if len(p) > 0 and len(r) > 0: titles.append(title) prices.append(Decimal(p[1:])) ratings.append(Decimal(r[:r.find(" ")])) except NoSuchElementException: pass return titles, prices, ratings
def get_refine_search_dict(self): if not refine_search_dict: left_nav_sections = self.driver.find_elements_by_locator( locators["left_nav_section"]) refine_search_by_titles = self.driver.find_elements_by_locator( locators["search_titles"]) for nav_section in left_nav_sections: if nav_section.text.lower() == "refine by": for search_title in refine_search_by_titles: title = WebElement(search_title).text.lower() refine_search_dict[title] = {} if "case color" in title: case_colors = WebElement( search_title).find_elements_by_locator( locators["colors"]) for color in case_colors: refine_search_dict[title][color.get_attribute( "title").lower()] = color elif "amazon prime" in title: option = "prime" name = WebElement( search_title).find_element_by_locator( locators["following_input_list"] ).get_attribute("name") loc = "css=input[type='checkbox'][name='" + name + "']" refine_search_dict[title][option] = CheckBox( self.driver, loc) elif "customer review" in title: customer_stars = self.driver.find_elements_by_locator( locators["stars"]) for star in customer_stars: option = star.get_attribute("class") num_of_stars = option[option.rfind("-") + 1:] refine_search_dict[title][num_of_stars + " stars & up"] = star else: try: input_list = WebElement( search_title).find_elements_by_locator( locators["following_input_list"]) for input_checkbox in input_list: attr = input_checkbox.get_attribute("name") name = WebElement( input_checkbox ).find_element_by_locator( locators["following_input_title"] ).text.lower() loc = "css=input[type='checkbox'][name='" + attr + "']" refine_search_dict[title][name] = loc except NoSuchElementException: raise Exception("exception, NOT inputs ") try: i = 1 option_list = WebElement( search_title).find_elements_by_locator( locators["following_option_list"]) for option in option_list: i += 1 name = WebElement( option).find_element_by_locator( locators["option_title"] ).text.lower() link = option.get_attribute("href") loc = "css=a[href='" + link[ link.find(".com") + 4:] + "']" refine_search_dict[title][name] = loc except NoSuchElementException: raise Exception("exception, NO lists ") return refine_search_dict