class AddAToDoText(unittest.TestCase): def setUp(self): options = EdgeOptions() options.use_chromium = True options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge Dev\\Application\\msedge.exe" dir = os.path.dirname(os.path.realpath(__file__)) edge_driver_path = dir + "\\edgedriver_win64\\msedgedriver.exe" self.service = Service(edge_driver_path) self.service.start() self.driver = Edge(options=options, service=self.service) self.driver.implicitly_wait(30) self.driver.maximize_window() self.driver.get("http://*****:*****@class='toggle']/following-sibling::label").get_attribute( "innerText") self.assertEqual("The test is adding this todo", addedToDoText) def tearDown(self): self.driver.quit() self.service.stop()
class HeaderText(unittest.TestCase): def setUp(self): options = EdgeOptions() options.use_chromium = True options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge Dev\\Application\\msedge.exe" dir = os.path.dirname(os.path.realpath(__file__)) edge_driver_path = dir + "\\edgedriver_win64\\msedgedriver.exe" self.service = Service(edge_driver_path) self.service.start() self.driver = Edge(options=options, service=self.service) self.driver.implicitly_wait(30) self.driver.maximize_window() self.driver.get("http://localhost:4200") def test_HeaderText(self): headerText = self.driver.find_element(By.CSS_SELECTOR, "h1").get_attribute("innerText") self.assertEqual("todos", headerText) def tearDown(self): self.driver.quit() self.service.stop()
def getcomponies(): """ Get Companies from web and write to excel file :return: """ _bases.kill_web_driver_edge() driver = Edge() componies = [] driver.get('https://www.dosab.org.tr/Alfabetik-Firmalar-Listesi') # Get links # links = [] # datalinks = driver.find_elements(By.XPATH, '/html/body/div[2]/div/ul/li/div/a') # for link in datalinks: # linkobj = { # 'link': link.get_attribute('href'), # 'name': link.text # } # links.append(linkobj) # Downlaod Mail Images # for complink in componies: # parsedlink = str(complink['link']).split('/') # mailimg = f'https://www.dosab.org.tr/dosyalar/emailler/{parsedlink[4]}_EMail.jpg' # wget.download(mailimg, "imgs") # OCR Image to text pytesseract.pytesseract.tesseract_cmd = r'C:\Users\abdul\AppData\Local\Tesseract-OCR\tesseract.exe' imgfiles = os.listdir('imgs') imgfiles.sort() for imgfile in imgfiles: compid = imgfile.split('_EMail.jpg')[0] driver.get(f'https://www.dosab.org.tr/Firma/{compid}') compname = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/h4').text img = cv2.imread(f'imgs/{imgfile}') emailtext = str(pytesseract.image_to_string(img, lang='eng')).replace( '\n\f', '') if '@' not in emailtext: emailtext = '' company = {'mail': emailtext, 'name': compname} componies.append(company) workbook = Workbook(excel_file_name) worksheet = workbook.add_worksheet('dosab') row = 0 hformat = workbook.add_format() hformat.set_bold() worksheet.write(row, 0, "Firma Adi", hformat) worksheet.write(row, 1, 'Mailler', hformat) row += 1 for comp in componies: worksheet.write(row, 0, comp["name"]) if '@' in comp['mail']: worksheet.write(row, 1, comp['mail']) row += 1 workbook.close() driver.close()
class SeleniumRuntime: """ This class works like a singleton containing a single instance of the browser environment Attributes: logger: logger instance gathered from logging module, acts like a singleton """ def __init__(self): self.logger = logging.getLogger(LOGGER_INSTANCE) if TARGET_BROWSER == 'chrome': self.browser = Chrome() elif TARGET_BROWSER == 'firefox': self.browser = Firefox() elif TARGET_BROWSER == 'edge': self.browser = Edge() def go_to_page(self, url): self.browser.get(url) def submit_form(self): form = self.browser.find_element_by_tag_name('form') form.submit() def fill_form(self, table): for field, value in table.items(): element = self.browser.find_element_by_id(field) element.clear() element.send_keys(value) def fill_selects(self, table): for field_name, field_value in table.items(): self.wait_for_element(field_value, By.XPATH).click() def click(self, value, by=By.ID): element = self.browser.find_element(by, value) element.click() def get_element(self, value, by=By.ID): return self.browser.find_element(by, value) def get_elements(self, value, by=By.ID): return self.browser.find_elements(by, value) def assert_presence(self, value, by=By.ID): try: self.browser.find_element(by, value) return True except NoSuchElementException: return False def back(self): self.browser.back() def forward(self): self.browser.forward() def refresh(self): self.browser.refresh() def current_title(self): return self.browser.title def current_url(self): return self.browser.current_url def wait_for_element(self, value, by=By.ID, timeout=30): return WebDriverWait(self.browser, timeout).until( expected_conditions.presence_of_element_located((by, value))) def wait_for_redirect(self, target_url, timeout=30): return WebDriverWait(self.browser, timeout).until( expected_conditions.url_to_be(target_url)) @staticmethod def assert_class(element, class_name): class_attr = element.get_attribute('class') return class_attr.find(class_name) >= 0 @staticmethod def assert_attribute(element, attribute_name, attribute_value): attr_value = element.get_attribute(attribute_name) return attr_value.find(attribute_value) >= 0
def getcomponies(): """ Get Companies from web and write to excel file :return: """ _bases.kill_web_driver_edge() driver = Edge() componies = [] driver.get('https://www.nosab.org.tr/firmalar/tr') alphabetslinks = [] for links in driver.find_elements(By.XPATH, '//*[@id="accordion-2"]/li/a'): link = { 'Sector': links.text, 'Name': links.get_attribute('href') } alphabetslinks.append(link) for anchor in alphabetslinks: driver.get(anchor['Name']) companies_sector = { 'Sector': anchor['Sector'], 'comps': [] } componies_count = len(driver.find_elements(By.XPATH, '/html/body/div[7]/div/div[2]/div[3]/ul/li/a')) for indx in range(1, componies_count + 1): comp = driver.find_element(By.XPATH, f'/html/body/div[7]/div/div[2]/div[3]/ul/li[{indx}]/a') comp.click() companies_sector['Sector'] = anchor['Sector'] company = { 'Name': driver.find_element(By.XPATH, '/html/body/div[7]/div/div[2]/div[1]/div').text, 'Data': str(driver.find_element(By.XPATH, '/html/body/div[7]/div/div[2]/div[4]').text) } companies_sector['comps'].append(company) driver.back() componies.append(companies_sector) row = 0 workbook = Workbook(excel_file_name) worksheet = workbook.add_worksheet('nosab') hformat = workbook.add_format() hformat.set_bold() hformat.set_align('center') hformat.set_align('vcenter') worksheet.write(row, 0, 'Firma Adi', hformat) worksheet.set_column('A:A', 100) worksheet.write(row, 1, 'Bilgileri', hformat) worksheet.set_column('B:B', 120) row += 1 fwarp = workbook.add_format() fwarp.set_text_wrap() fname_centralize = workbook.add_format() fname_centralize.set_align('center') for company in componies: if 'Sector' in company: worksheet.write(row, 0, company['Sector'], hformat) row += 1 if 'comps' in company: for comp in company['comps']: if 'Name' in comp: worksheet.write(row, 0, comp['Name'], fname_centralize) if 'Data' in comp: worksheet.write(row, 1, comp['Data'], fwarp) row += 1 if os.path.exists(excel_file_name): os.remove(excel_file_name) time.sleep(_bases.timeout) workbook.close() driver.close()
def get_componies(): """ Get Companies from web and write to excel file :return: """ _bases.kill_web_driver_edge() driver = Edge() componies = [] driver.get('https://bursaderiosb.com/uyeler') componies_data = driver.find_elements( By.XPATH, '//*[@id="main"]/div/div/div[2]/div') compscount = None if len(componies_data) > 0: compscount = len(componies_data) for i in range(1, compscount): company_anchor = driver.find_element( By.XPATH, f'//*[@id="main"]/div/div/div[2]/div[{i}]/div[2]/a') company_anchor.click() company = { 'Name': driver.find_element(By.XPATH, '//*[@id="main"]/div/div/div[1]').text, } company_data = str( driver.find_element( By.XPATH, '//*[@id="main"]/div/div/div[2]/div').text).split('\n') for compdata in company_data: mailstr = 'Mail:' if compdata.startswith(mailstr): company['Mail'] = compdata.split(mailstr)[1] phonestr = 'Telefon:' if compdata.startswith(phonestr): company['Tel'] = compdata.split(phonestr)[1] componies.append(company) driver.back() # region > Write to excel row = 0 workbook = Workbook(excel_file_name) worksheet = workbook.add_worksheet('Bursa Deri OSB') hformat = workbook.add_format() hformat.set_bold() hformat.set_align('center') hformat.set_align('vcenter') worksheet.write(row, 0, 'Firma Adi', hformat) worksheet.write(row, 1, 'Mail Adresi', hformat) worksheet.write(row, 2, 'Telefon', hformat) worksheet.set_column('A:A', 100) worksheet.set_column('B:B', 80) worksheet.set_column('C:C', 50) row += 1 for cmpy in componies: if 'Name' in cmpy: worksheet.write(row, 0, str(cmpy['Name'])) if 'Mail' in cmpy: worksheet.write(row, 1, str(cmpy['Mail'])) if 'Tel' in cmpy: worksheet.write(row, 2, str(cmpy['Tel'])) row += 1 if os.path.exists(excel_file_name): os.remove(excel_file_name) workbook.close() # endregion time.sleep(3) driver.quit()