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 __init__(self, data_dir_path='data', report_dir_path='reports', allow_indirect_matches=False, allow_duplicates=False, allow_blacklisted=False): super(GoogleSearcher, self).__init__(data_dir_path=data_dir_path, allow_indirect_matches=allow_indirect_matches, allow_duplicates=allow_duplicates) self._allow_blacklisted = allow_blacklisted Logger.info(self, 'Allowing blacklisted: {}'.format(self._allow_blacklisted)) self._black_list = self._source.get_black_list() self._engine = EngineFactory.get_engine('google_page') self._keys = self._source.get_keys() self._reporter = Reporter(report_dir_path, self.engine_name)
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_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
def __init__(self, data_dir_path): self._data_dir_path = data_dir_path self._cities_file_path = self._assemble_data_file_path('cities.txt') self._keys_file_path = self._assemble_data_file_path('keys.txt') self._black_list_filer_path = self._assemble_data_file_path('black_list.txt') Logger.info(self, 'Cities file path: {}'.format(self._cities_file_path)) Logger.info(self, 'Keys file path: {}'.format(self._keys_file_path)) Logger.info(self, 'Black list file path: {}'.format(self._black_list_filer_path))
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_black_list(self): Logger.info(self, 'Getting black list...') return self._get_items_from_file(self._black_list_filer_path)
def get_keys(self): Logger.info(self, 'Getting keys...') return self._get_items_from_file(self._keys_file_path)
def get_cities(self): Logger.info(self, 'Getting cities...') return self._get_items_from_file(self._cities_file_path)
def stop(self): self._driver.close() self._driver = None Logger.info(self, 'Engine stopped')
def start(self): self._driver = Chrome(executable_path=self._executable_path) self._driver.maximize_window() if self._url is not None: self._driver.get(self._url) Logger.info(self, 'Engine started')