def _get_single_result(self): Logger.debug(self, 'Getting single result...') result = [] try: name_output_xpath = '//div[@class="SPZz6b"]//span' self._wait_for_element_by_xpath(name_output_xpath, timeout=2) name_output = self._find_element_by_xpath(name_output_xpath) name = name_output.text address_output_class = 'LrzXr' self._wait_for_element_by_class_name(address_output_class, timeout=2) address_output = self._find_element_by_class_name( address_output_class) address = address_output.text result.append(name + '\n' + self._format_address(address) + '\n') except (TimeoutException, NoSuchElementException, ElementClickInterceptedException, StaleElementReferenceException) as e: Logger.error(self, e, self._get_single_result.__name__) raise GooglePageException('Failed to get single result') return result
def _get_results_from_current_page(self): results = [] i = 0 while True: i += 1 try: info_button_xpath = '//*[@id="kgwTable"]/tbody/tr[{}]/td[5]/button'.format( i) self._wait_for_clickability_by_xpath(info_button_xpath, timeout=3) info_button = self._find_element_by_xpath(info_button_xpath) info_button.click() name, address = self._get_result_details() results.append(name + '\n' + self._format_address(address) + '\n') self._close_details() except ElementClickInterceptedException as e: Logger.error(self, e, self._get_results_from_current_page.__name__) except KrkgwPageException as e: Logger.error(self, e, self._get_results_from_current_page.__name__) self._close_details() except (TimeoutException, NoSuchElementException): break return results
def _open_map(self): try: map_link = self._find_element_by_class_name('H93uF') map_link.click() except (TimeoutException, ElementClickInterceptedException) as e: Logger.error(self, e, self._open_map.__name__) raise GooglePageException('Failed to open a map')
def _get_results(self): results = [] if not self._is_result_present(): self._close_alert() return results results.extend(self._get_results_from_current_page()) page_links = self._get_page_links() if not page_links: return results else: page_links.pop(0) for page_number, page_link in enumerate(page_links, start=2): try: page_link = self._find_element_by_link_text(str(page_number)) page_link.click() except (NoSuchElementException, ElementClickInterceptedException) as e: Logger.error(self, e, self._get_results.__name__) continue else: results.extend(self._get_results_from_current_page()) return results
def _get_page_links(self): page_links = [] try: page_links = self._find_elements_by_class_name('page-link') except NoSuchElementException as e: Logger.error(self, e, self._get_page_links.__name__) finally: return [ page_link for page_link in page_links if page_link.text not in ('<<', '<', '>', '>>') ]
def _close_details(self): try: close_button_xpath = '//*[@id="myModal"]/div/div/div[1]/button' self._wait_for_clickability_by_xpath(close_button_xpath) sleep(0.3) close_button = self._find_element_by_xpath(close_button_xpath) close_button.click() sleep(1) except (TimeoutException, NoSuchElementException, ElementClickInterceptedException) as e: Logger.error(self, e, self._get_results.__name__) raise KrkgwPageException('Failed to close details')
def _enter_query(self, city, key): Logger.debug(self, "Searching for city: '{}' key: '{}'".format(city, key)) sleep(1.2) try: self._driver.get(url=self._url) search_input = self._find_element_by_name('q') search_input.send_keys('{} {} adres'.format(city, key)) search_input.send_keys(Keys.ENTER) except NoSuchElementException as e: Logger.error(self, e, self._enter_query.__name__) raise GooglePageException('Failed to enter query')
def _enter_query(self, city_name): Logger.debug(self, "Searching for city name: '{}'".format(city_name)) try: search_input = self._find_element_by_id('kgw') search_input.clear() search_input.send_keys(city_name) sleep(0.3) search_button = self._find_element_by_id('search') search_button.click() except (NoSuchElementException, ElementClickInterceptedException) as e: Logger.error(self, e, self._enter_query.__name__) raise KrkgwPageException('Failed to enter query')
def _get_result(self): Logger.debug(self, 'Getting result...') sleep(2) try: distance_output_xpath = '//*[@id="section-directions-trip-0"]/div/div[1]/div[1]/div[2]/div' self._wait_for_element_by_xpath(distance_output_xpath, timeout=10) distance_output = self._find_element_by_xpath( distance_output_xpath) return distance_output.text.strip() except (TimeoutException, NoSuchElementException, ElementClickInterceptedException, StaleElementReferenceException) as e: Logger.error(self, e, self._get_result.__name__) raise MapPageException('Failed to get result')
def _get_result_details(self): try: name_output_xpath = '//*[@id="zawartosc"]/table[1]/tbody/tr[3]/td[2]' self._wait_for_visibility_by_xpath(name_output_xpath, timeout=10) name_output = self._find_element_by_xpath(name_output_xpath) name = name_output.text address_output_xpath = '//*[@id="zawartosc"]/table[1]/tbody/tr[4]/td[2]' self._wait_for_visibility_by_xpath(address_output_xpath, timeout=10) address_output = self._find_element_by_xpath(address_output_xpath) address = address_output.text except (TimeoutException, NoSuchElementException) as e: Logger.error(self, e, self._get_result_details.__name__) raise KrkgwPageException('Failed to get result details') return name, address
def _enter_query(self, main_city, target_city): Logger.debug( self, "Searching distance from main city: '{}' to target city: '{}'". format(main_city, target_city)) sleep(2) try: from_input = self._find_element_by_xpath( '//*[@id="sb_ifc50"]/input') from_input.clear() from_input.send_keys(main_city) to_input = self._find_element_by_xpath('//*[@id="sb_ifc51"]/input') to_input.clear() to_input.send_keys(target_city) to_input.send_keys(Keys.ENTER) except NoSuchElementException as e: Logger.error(self, e, self._enter_query.__name__) raise MapPageException('Failed to enter query')
def search(self): Logger.info(self, 'Searching...') self._reporter.generate_new_report_file_path() self._add_city_header(self._main_city) self._engine.start() for target_city in self._cities: results = None try: results = self._engine.search(self._main_city, target_city) except EngineException as e: Logger.error(self, e, self.search.__name__) finally: self._add_results(results, target_city) self._reporter.save_report(self._results) self._reporter.save_report(self._results) self._reporter.save_to_excel(self._results, self._main_city) self._engine.stop()
def search(self): Logger.info(self, 'Searching...') self._reporter.generate_new_report_file_path() self._engine.start() for city in self._cities: self._add_city_header(city) results = [] try: results = self._engine.search(self._get_city_name(city)) except EngineException as e: Logger.error(self, e) finally: self._add_results(results, city) self._reporter.save_report(self._results) self._add_results_count() self._reporter.save_report(self._results) self._engine.stop()
def _get_multiple_results(self): Logger.debug(self, 'Getting multiple results...') results = [] result_link_class = 'dbg0pd' try: self._open_map() self._wait_for_element_by_class_name(result_link_class, 2) result_links = self._find_elements_by_class_name(result_link_class) except (TimeoutException, NoSuchElementException, GooglePageException) as e: Logger.error(self, e, self._get_multiple_results.__name__) return results for result_link in result_links: try: name, address = self._get_one_of_multiple_results(result_link) results.append(name + '\n' + self._format_address(address) + '\n') except GooglePageException as e: Logger.error(self, e, self._get_multiple_results.__name__) return results
def _get_one_of_multiple_results(self, result_link): try: result_link.click() sleep(1) name_output_xpath = '//div[@class="SPZz6b"]//span' self._wait_for_element_by_xpath(name_output_xpath, timeout=2) name_output = self._find_element_by_xpath(name_output_xpath) name = name_output.text address_output_class = 'LrzXr' self._wait_for_element_by_class_name(address_output_class, timeout=1) address_output = self._find_element_by_class_name( address_output_class) address = address_output.text except (TimeoutException, NoSuchElementException, ElementClickInterceptedException, StaleElementReferenceException) as e: Logger.error(self, e, self._get_one_of_multiple_results.__name__) raise GooglePageException('Failed to get one of multiple results') return name, address