def go(): for data in data_list: driver.implicitly_wait(1.5) try: driver.find_element_by_xpath('//input[@name="name"]').clear() driver.find_element_by_xpath('//input[@name="name"]').send_keys( data[0]) except NoSuchElementException: wait('Enter') driver.find_element_by_xpath('//input[@name="name"]').clear() driver.find_element_by_xpath('//input[@name="name"]').send_keys( data[0]) driver.find_element_by_xpath('//button[@title="Szukaj"]').click() sleep(1) try: print( driver.find_element_by_xpath( '//*[@id="productGrid_table"]/tbody/tr/td[7]').text) except NoSuchElementException: print('') undone.append(data[0]) print(f'\n\nNiezrobione:') print('\n'.join(undone)) done.clear() undone.clear() data_list.clear()
def go(): for data in data_list: driver.implicitly_wait(1.5) driver.find_element_by_xpath('//input[@name="sku"]').clear() driver.find_element_by_xpath('//input[@name="sku"]').send_keys(data[0]) driver.find_element_by_xpath('//button[@title="Szukaj"]').click() try: x = driver.find_element_by_xpath('//*[@id="messages"]/ul//span').text except NoSuchElementException: x = '' if x == 'Produkt został zapisany.': sleep(2) pyautogui.click(300, 605) else: pyautogui.click(300, 552) try: driver.find_element_by_xpath('//*[@id="content"]/div/div[2]/h3').get_attribute("innerText") except NoSuchElementException: undone.append(data[0]) continue driver.find_element_by_xpath('//input[@name="product[search_keywords]"]').send_keys(' #PrezentGalaxyBuds') driver.find_element_by_xpath('//button[@title="Zapisz"]').click() print(f'\n\nNiezrobione:') print('\n'.join(undone)) done.clear() undone.clear() data_list.clear()
def go(): for data in data_list: driver.implicitly_wait(3) driver.find_element_by_xpath('//input[@name="sku"]').clear() driver.find_element_by_xpath('//input[@name="sku"]').send_keys(data) driver.find_element_by_xpath('//button[@title="Szukaj"]').click() try: x = driver.find_element_by_xpath('//*[@id="messages"]/ul//span').text except NoSuchElementException: x = '' if x == 'Produkt został zapisany.': sleep(2) pyautogui.click(300, 605) else: pyautogui.click(300, 552) try: model = driver.find_element_by_xpath('//*[@id="content"]/div/div[2]/h3').get_attribute("innerText") except NoSuchElementException: undone.append(data) continue model = model[model.find('AMICA')+6:model.find('(')-1] driver.execute_script('''window.open("about:blank", "_blank");''') driver.switch_to.window(driver.window_handles[1]) driver.get(f'https://www.ceneo.pl/;szukaj-{model}?nocatnarrow=1') ceneo_name = driver.find_element_by_xpath('//div[@class="cat-prod-row__desc"]//a').text mod = model.replace(" ", "") ceneo_name = ceneo_name.replace(" ", "") print(f'Model: {mod.lower()} -- Ceneo: {ceneo_name.lower()}) -- {mod.lower() in ceneo_name.lower()}') if mod.lower() in ceneo_name.lower(): link = driver.find_element_by_xpath('//div[@class="cat-prod-row__desc"]//a').get_attribute('href') driver.execute_script("window.close('');") driver.switch_to.window(driver.window_handles[0]) driver.find_element_by_name('product[link_ceneo]').clear() driver.find_element_by_name('product[link_ceneo]').send_keys(link) driver.find_element_by_xpath('//button[@title="Zapisz"]').click() done.append(model) else: driver.execute_script("window.close('');") driver.switch_to.window(driver.window_handles[0]) driver.find_element_by_xpath('//button[@title="Powrót"]').click() undone.append(model) print('Zrobione:') print('\n'.join(done)) print(f'\n\nNiezrobione:') print('\n'.join(undone)) done.clear() undone.clear() data_list.clear()
def go(): for data in data_list: driver.implicitly_wait(1.5) name = 'name' try: driver.find_element_by_xpath(f'//input[@name="{name}"]').clear() driver.find_element_by_xpath(f'//input[@name="{name}"]').send_keys( data[0]) except NoSuchElementException: wait('Enter') driver.find_element_by_xpath(f'//input[@name="{name}"]').clear() driver.find_element_by_xpath(f'//input[@name="{name}"]').send_keys( data[0]) driver.find_element_by_xpath('//button[@title="Szukaj"]').click() sleep(1) i = 1 while True: try: if data[0] in driver.find_element_by_xpath( f'//*[@id="productGrid_table"]/tbody/tr[{i}]/td[3]' ).text: done.append( driver.find_element_by_xpath( f'//*[@id="productGrid_table"]/tbody/tr[{i}]/td[2]' ).text) break i += 1 except NoSuchElementException: undone.append(data[0]) break for i in range(0, len(done), 5): try: step = f'{done[i]}' step += f', {done[i + 1]}' step += f', {done[i + 2]}' step += f', {done[i + 3]}' step += f', {done[i + 4]}' except IndexError: pass print(step) print(f'\n\nNiezrobione:') print('\n'.join(undone)) done.clear() undone.clear() data_list.clear()
def go(): for data in data_list: driver.implicitly_wait(10) try: driver.find_element_by_xpath('//input[@name="sku"]').clear() driver.find_element_by_xpath('//input[@name="sku"]').send_keys(data[0]) except NoSuchElementException: wait('Enter') driver.find_element_by_xpath('//input[@name="sku"]').clear() driver.find_element_by_xpath('//input[@name="sku"]').send_keys(data[0]) driver.find_element_by_xpath('//button[@title="Szukaj"]').click() try: x = driver.find_element_by_xpath('//*[@id="messages"]/ul//span').text except NoSuchElementException: x = '' if x == 'Produkt został zapisany.': sleep(2) pyautogui.click(300, 605) else: pyautogui.click(300, 552) try: driver.find_element_by_xpath('//*[@id="content"]/div/div[2]/h3').get_attribute("innerText") except NoSuchElementException: undone.append(data[0]) continue driver.find_element_by_xpath('//a[@title="Porównywarki cenowe"]').click() driver.find_element_by_xpath('//select[@id="export_marketing"]/option[1]').click() driver.find_element_by_xpath('//button[@title="Zapisz"]').click() print(f'\n\nNiezrobione:') print('\n'.join(undone)) done.clear() undone.clear() data_list.clear()
def bosch(report_lab, page_address, model): driver.get(page_address) sleep(3) # po wczytaniu strona jest przez krótką chwilę zakryta przez inne okno driver.implicitly_wait(3) """ OBRAZKI PRODUKTU """ i = 1 first = '' while True: try: img = driver.find_element_by_xpath(f'//div[@class="slick-track"]/*[{i}]//picture/img').get_attribute( 'src').replace('/600x337', '/1200x675') print(img) if i == 1: first = img else: if first == img: break prod_img(f'{file_path}/{model}', img, i+1) i += 1 except NoSuchElementException: break """OPIS KRÓTKI""" try: shorts = driver.find_element_by_xpath('//div[contains(@class, "morecontent")]//ul').text.split('\n') for i in range(len(shorts)): shorts[i] = '<li>' + shorts[i][:shorts[i].find(':')] + '</li>' short = '<ul>' + ''.join(shorts) + '</ul>' except NoSuchElementException: short = '' print('NoSuchElementException. Brak opisu krótkiego') """OPIS GRAFICZNY""" desc = '' i = 1 while True: print(f'g_desc: {i}') try: text = driver.find_element_by_xpath(f'//*[@id="section-highlights"]/*[{i}]').text try: while True: img = driver.find_element_by_xpath( f'//*[@id="section-highlights"]/*[{i}]//picture//img').get_attribute( "src") driver.find_element_by_xpath( f'//*[@id="section-highlights"]/*[{i}]//picture//img').location_once_scrolled_into_view if i == 2: sleep(2) img = driver.find_element_by_xpath( f'//*[@id="section-highlights"]/*[{i}]//picture//img').get_attribute("src") else: pass if 'Feature_Icons' in img: try: img = driver.find_element_by_xpath( f'//*[@id="section-highlights"]/*[{i}]//div[contains(@class, "mediagallery-slide")][2]//picture//img').get_attribute( "src") except NoSuchElementException: pass replacements = ['/600x337', '/600x'] for replacement in replacements: img = img.replace(replacement, '') res = requests.get(img) with open(f'{file_path}/{model}/obrazki_opisu/{i}.jpg', 'wb') as file_format: file_format.write(res.content) im = Image.open(f'{file_path}/{model}/obrazki_opisu/{i}.jpg') width, height = im.size if width > 480: ratio = width / 480 new_width = round(width / ratio) new_height = round(height / ratio) im = im.resize((new_width, new_height)) try: im.save(f'{file_path}/{model}/obrazki_opisu/{i}.jpg') except OSError: im = im.convert('RGB') im.save(f'{file_path}/{model}/obrazki_opisu/{i}.jpg') break except NoSuchElementException: pass i += 1 if text == '': continue replacements = ['Poprzednie', 'Następne', '1/2', '2/2', '1/3', '2/3', '3/3', 'Dowiedz się więcej', ' Powrót', 'Dalej'] for replacement in replacements: text = text.replace(replacement, '') text = text.split('\n') text = [e for e in text if e] direction = 'left' if i % 2 == 0 else 'right' # tekst po lewej/prawej stronie względem zdjęcia if len(text) == 1: print('jeden') desc += f'<div><h1 class="important-header" style="text-align: center;">{text[0]}</h1></div>' elif len(text) == 2: print('dwa') desc += f"""<div class="two-col-asymmetrically"><div class="{direction}-side"><h2 class="important-header"> {text[0]}</h2> <p style="font-size: large;">{text[1]}</p></div> <img alt="" src="https://matrixmedia.pl/media/wysiwyg/Bosch/{model}/{i}.jpg" /></div> """ elif len(text) == 3: print('trzy') desc += f"""<div class="two-col-asymmetrically"><div class="{direction}-side"><h2 class="important-header"> {text[0]}</h2> <p style="font-size: large;">{text[1]} <br/><br/> <small>{text[2]}</small></p></div> <img alt="" src="https://matrixmedia.pl/media/wysiwyg/Bosch/{model}/{i}.jpg" /></div> """ else: print(f'\n------\n{text}\n------\n') except NoSuchElementException: break desc = '<div class="product-description-section">' + desc + '</div>' """OPIS TECHNICZNY""" driver.get(page_address + '#/Tabs=section-technicalspecs/') driver.find_element_by_xpath('//div[@id="tech-data"]//span[contains(text(), "Specyfikacja techniczna")]').click() # znajdź który div zawiera informacje techniczne i = 1 while True: if 'specyfikacja techniczna' in driver.find_element_by_xpath(f'//div[@id="tech-data"]/*[{i}]').text.lower(): break else: i += 1 # zbierz dane techniczne j = 1 tech = [] driver.implicitly_wait(0.5) while True: try: driver.find_element_by_xpath(f'//div[@id="tech-data"]/*[{i}]/section/div[{j}]/a').click() sleep(0.5) category_name = driver.find_element_by_xpath(f'//div[@id="tech-data"]/*[{i}]/section/div[{j}]/a').text tech.append(f'<tr class="specs_category"><td colspan="2">{category_name}</td></tr>') except NoSuchElementException: break k = 1 while True: try: name = driver.find_element_by_xpath(f'//div[@id="tech-data"]/*[{i}]/section/div[{j}]//tr[{k}]/th').text value = driver.find_element_by_xpath(f'//div[@id="tech-data"]/*[{i}]/section/div[{j}]//tr[{k}]/td').text if value == '': icon = driver.find_element_by_xpath( f'//div[@id="tech-data"]/*[{i}]/section/div[{j}]//tr[{k}]/td').get_attribute('class') value = 'Tak' if icon.lower() == 'checked' else 'Nie' tech.append(f'<tr><td class="c_left">{name}</td><td class="c_left">{value}</td></tr>') k += 1 except NoSuchElementException: break j += 1 tech = ['<table id="plan_b" class="data-table"><tbody>'] + tech + ['</tbody></table>'] tech = ''.join(tech) return [desc, short, tech]
def insert_data(report_label, product, model=None, descriptions=None, brand=None): folder_name = model set_images = False # parametr służący w przyszłości do wybierania czy algorytm ma dodawać zdjęcia opisu do bazy if descriptions is None: descriptions = [1, '', ''] try: driver.find_element_by_css_selector( 'td.a-right button').click() # Dodaj produkt except NoSuchElementException: pass db = mysql.connector.connect(host="localhost", user="******", database="test") cursor = db.cursor() cursor.execute("SELECT * FROM attributes_sets") db_results = cursor.fetchall() results = [] for i in range(len(db_results)): results.append(list(db_results[i])) for j in range(len(results[i])): if results[i][j] is None: results[i][j] = '' attr_set = '' # szukanie zestawu cech for i in range(len(results)): if results[i][1].lower() in product[1].lower( ) and 'zabudo' in product[6].lower(): attr_set = results[i][4] weight = str(results[i][3]) if weight == '': weight = '1' break elif results[i][1].lower() in product[1].lower(): attr_set = results[i][2] weight = str(results[i][3]) if weight == '': weight = '1' break else: weight = '1' try: # zaznaczanie zestawu cech driver.find_element_by_xpath( f'//*[@id="attribute_set_id"]/option[text()="{attr_set}"]').click( ) except NoSuchElementException: print( f'Nie znaleziono zestawu cech. Zrób to ręcznie i wciśnij Enter ({product[1]})\n' ) wait('enter') driver.find_element_by_css_selector( 'button[title="Kontynuuj"]').click() # Konunuuj driver.implicitly_wait( 3 ) # Zawsze czekaj 3 sekundy przed wyświetleniem błędu na wczytanie elementów strony driver.find_element_by_name('product[name]').send_keys( product[1]) # umieść nazwę produktu # umieść opis graficzny driver.find_element_by_name('product[description]').send_keys( descriptions[0]) # jeżeli nie ma opisu, to nie ma też grafik if set_images: try: driver.find_element_by_xpath( '(//button[@title="Edytor WYSIEYG"])[1]').click( ) # kliknij na edytor WYSIEYG if folder_name is not None: driver.find_element_by_xpath( '/html/body/div[2]/table[2]/tbody/tr/td[2]/div/div[' '1]/div/span/div/span/table/tbody/tr[1]/td/div/span/table[2]/tbody/tr/td[' '24]/a/span[1]').click() # kliknij na "Wstawianie obrazka" sleep(1) # poczekaj sekundę, aż wczyta się okno pyautogui.click( 1135, 470 ) # kliknij na "Browse" (nie znalazłem lepszego sposobu) sleep( 7 ) # poczekaj 7 sekund (+ ew. 3 sekundy z metody "implicitly_wait") na wczytanie treści try: driver.find_element_by_xpath( f'//a/span[text()="{brand}"]').click( ) # wybierz folder z nazwą marki driver.find_element_by_xpath( f'//a/span[text()="{brand}"]').click( ) # wybierz folder z nazwą marki except ElementNotInteractableException: print("Spróbuj ponownie (enter)") wait('Enter') driver.find_element_by_xpath( f'//a/span[text()="{brand}"]').click() driver.find_element_by_xpath( f'//a/span[text()="{brand}"]').click() sleep(3) try: driver.find_element_by_xpath( '/html/body/div[1]/table[2]/tbody/tr/td[2]/div/div/div/div/div/div/div[' '2]/div/div[2]/table/tbody/tr/td[2]/button[1]').click( ) # Utwórz folder sleep(1) pyautogui.click( 900, 160 ) # ustaw focus na oknie do wpisywania nazwy folderu pyautogui.typewrite(folder_name) # wprowadź nazwę folderu pyautogui.press('Enter') # zatwierdź enterem sleep(12) driver.find_element_by_xpath( f'//a/span[text()="{folder_name}"]').click( ) # kliknij na nazwę folderu sleep(2) pyautogui.press('Home') # wróć na górę sleep(2) pyautogui.click(900, 290) # kliknij na "Choose files" pyautogui.click(900, 305) # drugi dla pewności sleep(2.5) # poczekaj na załadownie okna pyautogui.hotkey( 'ctrl', 'l') # Ctrl + L (przejdź do adresu folderu) # wprowadź ścieżkę folderu z obrazkami do opisu pyautogui.typewrite( f'C:/Users/user/PycharmProjects/Adding-automating-products/Imgs/{folder_name}/obrazki_opisu' ) pyautogui.press('enter') # zatwierdź enterem sleep(2) pyautogui.click(300, 300) pyautogui.hotkey('ctrl', 'a') # Ctrl + A (zaznacz wszystko) pyautogui.press('enter') # zatwierdź Enterem pyautogui.press('Home') # wróć na górę sleep(7) except UnexpectedAlertPresentException: report_label[ 'text'] += f'Produkt {product[1]} już jest dodany.\n' pyautogui.click(1150, 180) except NoSuchElementException: pass finally: try: driver.find_element_by_xpath('//*[@id="browser_window_close"]' ).click() # zamknij jedno okno driver.find_element_by_xpath('/html/body/div[6]/div/a[5]' ).click() # zamknij drugie okno driver.find_element_by_xpath('//button[@title="Submit"]' ).click() # zamknij trzecie okno except NoSuchElementException: pass driver.find_element_by_name('product[short_description]').send_keys( descriptions[1]) # wprowadź krótki opis if not descriptions[1] == '': try: driver.find_element_by_xpath( '(//button[@title="Edytor WYSIEYG"])[2]').click( ) # otwórz edytor WYSIEYG # zamknij edytory WYSIEYG (żeby sformatować tekst) driver.find_element_by_xpath('//button[@title="Submit"]').click() except NoSuchElementException: print( "Otwórz Edytor WYSIEYG dla opisu krótkiego, zatwierdź i wciśnij Enter" ) wait('Enter') driver.find_element_by_name('product[sku]').send_keys( product[2]) # wprowadzenie Eanu driver.find_element_by_name('product[weight]').send_keys( weight) # wprowadzenie wagi driver.find_element_by_xpath( '//*[@id="status"]/option[3]').click() # status: Nieaktywny # jeżeli marka jest znana - zaznacz ją if brand != 'nie wykryto': try: driver.find_element_by_xpath( f'//*[@id="manufacturer"]/option[text()="{brand}"]').click() except NoSuchElementException: try: driver.find_element_by_xpath( f'//*[@id="manufacturer"]/option[text()="{brand.upper()}"]' ).click() except NoSuchElementException: pass driver.find_element_by_name('product[url_key]').send_keys( product[1]) # wprowadzenie klucza url driver.find_element_by_name('product[manufacturer_code]').send_keys( product[0]) # wprowadzenie klucza producenta # znajdź link do aukcji ceneo driver.execute_script('''window.open("about:blank", "_blank");''') driver.switch_to.window(driver.window_handles[1]) driver.get(f'https://www.ceneo.pl/;szukaj-{model}?nocatnarrow=1') try: ceneo_name = driver.find_element_by_xpath( '//div[@class="cat-prod-row__desc"]//a').text # weź tylko litery z dwóch nazw modeli (ceneo_name oraz mod) buffor = '' for i in range(len(ceneo_name)): if ceneo_name[i].isalpha(): buffor += ceneo_name[i] ceneo_name = buffor mod = '' for i in range(len(model)): if model[i].isalpha(): mod += model[i] # sprawdzenie ręczne czy analiza jest poprawna print( f'Model: {mod.lower()} -- Ceneo: {ceneo_name.lower()} -- {mod.lower() in ceneo_name.lower()}' ) if mod.lower() in ceneo_name.lower(): link = driver.find_element_by_xpath( '//div[@class="cat-prod-row__desc"]//a').get_attribute('href') driver.execute_script("window.close('');") driver.switch_to.window(driver.window_handles[0]) driver.find_element_by_name('product[link_ceneo]').send_keys(link) else: driver.execute_script("window.close('');") driver.switch_to.window(driver.window_handles[0]) except NoSuchElementException: driver.execute_script("window.close('');") driver.switch_to.window(driver.window_handles[0]) driver.find_element_by_name('product[tech_description]').send_keys( descriptions[2]) # wprowadź opis techniczny if not descriptions[2] == '': driver.find_element_by_xpath('(//button[@title="Edytor WYSIEYG"])[3]' ).click() # otwórz edytor WYSIEYG driver.find_element_by_xpath( '//button[@title="Submit"]').click() # zamknij edytor WYSIEYG # zazancz centralę w punktach odbioru try: driver.find_element_by_xpath( f'//*[@id="pickup_store"]/option[1]').click() except NoSuchElementException: pass if 'sony' in product[1].lower(): sony_rec = [2, 3, 4, 7, 8, 9, 10, 11, 21, 25] for ele in sony_rec: driver.find_element_by_xpath( f'//*[@id="pickup_store"]/option[{ele}]').click() if 'samsung' in product[1].lower(): samsing_rec = [5, 6, 12, 13, 14, 15, 16, 17, 18, 19, 20, 23] for ele in samsing_rec: driver.find_element_by_xpath( f'//*[@id="pickup_store"]/option[{ele}]').click() if 'huawei' in product[1].lower(): huawei_rec = [24] for ele in huawei_rec: driver.find_element_by_xpath( f'//*[@id="pickup_store"]/option[{ele}]').click() # wyłącz Huaweia z negocjacji cen driver.find_element_by_xpath( '//select[@name="product[price_negotiation_hide]"]/option[1]' ).click() # podobno nie są już potrzebne, więc zakomentowałem, ale zostawiam w razie W # driver.find_element_by_name('product[search_keywords]').send_keys(f'{product[1]} {product[2]}') # słowa kluczowe # Ceny driver.find_element_by_tag_name('body').send_keys( Keys.HOME ) # powrót na górę strony, aby można było odnaleźć szukane elementy sleep(0.5) # poczekaj na działanie klawisza Home driver.find_element_by_css_selector( 'a[title="Ceny"]').click() # przejdź do zakładki "Ceny" driver.find_element_by_name('product[price]').send_keys( '9999,99') # wprowadź domyślną cenę 9999,99 driver.find_element_by_css_selector( '#tax_class_id > option:nth-child(2)').click() # Klasa podatku: Brak # Matrix Media driver.find_element_by_css_selector('a[title="Matrix Media"]').click( ) # przejdź do zakładki "Matrix Media" # jeżeli reguła i priorytet nadawcy są poprawnie wpisane, to zaznacz je try: driver.find_element_by_xpath( f'//*[@id="rule"]/option[text()="{product[4]}"]').click() except NoSuchElementException: report_label['text'] += "Błędnie wpisana reguła\n" try: driver.find_element_by_xpath( f'//*[@id="supplier"]/option[text()="{product[5]}"]').click() except NoSuchElementException: report_label['text'] += "Błędnie wpisany dostawca\n" # Porównywarki cenowe driver.find_element_by_css_selector( 'a[title="Porównywarki cenowe"]').click() driver.find_element_by_xpath('//*[@id="export_ceneo"]/option[1]').click() # Kategorie driver.find_element_by_css_selector('a[title="Kategorie"]').click() categories(product[1], product[6], attr_set) # Zbyt rozległe, żeby to zostawiać tutaj # Zestaw cech try: driver.find_element_by_css_selector(f'a[title="{attr_set}"]').click() attributes(descriptions[2], brand) except NoSuchElementException: pass # Obrazki produktu driver.find_element_by_css_selector('a[title="Obrazki produktu"]').click() # jeżeli nie ma opisu, to nie ma też grafik if not descriptions[0] == 1: driver.find_element_by_tag_name('body').send_keys( Keys.HOME ) # powrót na górę strony, aby można było odnaleźć szukane elementy sleep(2) pyautogui.click(500, 570) pyautogui.click(500, 620) pyautogui.click(500, 650) # trzy razy dla pewności, bo bywało różnie sleep(2) pyautogui.hotkey('ctrl', 'l') # Ctrl + L pyautogui.keyUp('ctrl') pyautogui.keyUp('l') # puszczenie Ctrl + L # wprowadzenie ścieżki gdzie znajdują się obrazki produktu pyautogui.typewrite( f'C:/Users/user/PycharmProjects/Adding-automating-products/Imgs/{folder_name}/obrazki_produktu' ) pyautogui.press('enter') # Enter sleep(0.5) pyautogui.click(300, 300) pyautogui.hotkey('ctrl', 'a') # Ctrl + A pyautogui.press('enter') # Enter sleep(1) sleep(2) driver.find_element_by_css_selector( 'button[title="Zapisz i kontynuuj edycję"]').click() # Zapisz wait('enter') sleep(3) driver.find_element_by_css_selector( 'button[title="Zapisz"]').click() # Zapisz sleep(3) report_label['text'] += f'Dodano {product[1]}\n'
def go(): for data in data_list: driver.implicitly_wait(3) driver.find_element_by_xpath('//input[@name="sku"]').clear() driver.find_element_by_xpath('//input[@name="sku"]').send_keys(data[0]) driver.find_element_by_xpath('//button[@title="Szukaj"]').click() try: x = driver.find_element_by_xpath( '//*[@id="messages"]/ul//span').text except NoSuchElementException: x = '' if x == 'Produkt został zapisany.': sleep(2) pyautogui.click(300, 552) else: pyautogui.click(300, 502) try: driver.find_element_by_xpath( '//*[@id="content"]/div/div[2]/h3').get_attribute("innerText") except NoSuchElementException: undone.append(data[0]) continue driver.find_element_by_xpath('//a[@title="Zestawy"]').click() for ean in eans_in_set: sleep(0.5) driver.find_element_by_xpath( '//button[@title="Dodaj nowy zestaw"]').click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//input[@class="input-text"]' ).send_keys(set_name[eans_in_set.index(ean)]) driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//input[contains(@class, "validate-date-range")][1]' ).send_keys(date_from) driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//input[contains(@class, "validate-date-range")][2]' ).send_keys(date_to) driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//input[contains(@name, "[sort_order]")]' ).send_keys(sorting_order) driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//select[contains(@name, "[customer_groups]")]/option[1]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//select[contains(@name, "[customer_groups]")]/option[2]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//select[contains(@name, "[customer_groups]")]/option[3]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//select[contains(@name, "[customer_groups]")]/option[4]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//select[contains(@name, "[store_ids]")]/option[1]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//select[contains(@name, "[store_ids]")]//option[contains(text(), "Default Store View")]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//button[@title="Dodaj nowy produkt"]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//input[@name="sku"]' ).send_keys(ean) driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//button[@title="Szukaj"]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//tr[@title="#"]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//button[@title="Dodaj wybrane produkt(y) do opcji"]' ).click() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//table[@class="border"]//tr[1]//input[contains(@name, "[discount_price]")]' ).clear() driver.find_element_by_xpath( '//div[contains(@id, "bundlediscount_option")][1]//table[@class="border"]//tr[1]//input[contains(@name, "[discount_price]")]' ).send_keys(promo_price[eans_in_set.index(ean)]) driver.find_element_by_xpath('//button[@title="Zapisz"]').click() done.append(data[0]) print(f'\n\nNiezrobione:') print('\n'.join(undone)) done.clear() undone.clear() data_list.clear()
def go(): i = 1 for data in data_list: driver.implicitly_wait(8) driver.find_element_by_xpath(f'//input[@name="{SEARCH_BY}"]').clear() driver.find_element_by_xpath( f'//input[@name="{SEARCH_BY}"]').send_keys(data[0]) driver.find_element_by_xpath('//button[@title="Szukaj"]').click() sleep(1) for j in range( 1, len( driver.find_elements_by_xpath( f'//table[@id="productGrid_table"]/tbody/tr')) + 1): if driver.find_elements_by_xpath( f'//table[@id="productGrid_table"]/tbody/tr' )[0].text == 'Nie znaleziono wpisów.': undone.append(data[0]) break try: if driver.find_element_by_xpath(f'//table[@id="productGrid_table"]/tbody/tr[{j}]/td[7]').text == data[0]\ and driver.find_element_by_xpath(f'//table[@id="productGrid_table"]/tbody/tr[{j}]/td[10]').text == 'Katalog, wyszukiwanie': driver.find_element_by_xpath( f'//table[@id="productGrid_table"]/tbody/tr[{j}]' ).click() break except StaleElementReferenceException: sleep(1) if driver.find_element_by_xpath(f'//table[@id="productGrid_table"]/tbody/tr[{j}]/td[7]').text == data[0] \ and driver.find_element_by_xpath(f'//table[@id="productGrid_table"]/tbody/tr[{j}]/td[10]').text == 'Katalog, wyszukiwanie': driver.find_element_by_xpath( f'//table[@id="productGrid_table"]/tbody/tr[{j}]' ).click() break try: driver.implicitly_wait(3) driver.find_element_by_xpath( '//*[@id="content"]/div/div[2]/h3').get_attribute("innerText") except NoSuchElementException: undone.append(data[0]) continue """ Początek instrukcji """ print(f"-- {i} -- {data[0]} --") i += 1 instrukcje(data[0]) """ Koniec instrukcji """ driver.find_element_by_xpath('//button[@title="Zapisz"]').click() try: driver.find_element_by_xpath('//button[@title="Dodaj produkt"]') except NoSuchElementException: driver.find_element_by_xpath('//button[@title="Powrót"]').click() undone.append(data[0]) print(f'\n\nNiezrobione:') print('\n'.join(undone)) done.clear() undone.clear() data_list.clear()
def electrolux(report_lab, product, model): driver.implicitly_wait(2) driver.get(product[6]) driver.find_element_by_xpath( '/html/body/div[2]/form/div[4]/div/div/div/div[1]/div/div[2]/table/tbody/tr[2]/td/input' ).send_keys('*****@*****.**') driver.find_element_by_xpath( '/html/body/div[2]/form/div[4]/div/div/div/div[1]/div/div[2]/table/tbody/tr[4]/td/input' ).send_keys('raza849') driver.find_element_by_xpath( '/html/body/div[2]/form/div[4]/div/div/div/div[1]/div/div[2]/table/tbody/tr[7]/td/input' ).click() """ Obrazki produktu """ imgs_desc = driver.find_elements_by_xpath( '//*[@id="Family"]//div[@class="colPicture"]//img') prod_img_names = [] for link in imgs_desc: prod_img_name = link.get_attribute('src') prod_img_name = prod_img_name[prod_img_name.rfind('/') + 1:] prod_img_name = prod_img_name[:prod_img_name.find('.')] prod_img_names.append(prod_img_name) """ Opis graficzny """ tiles = driver.find_elements_by_xpath( '//div[@id="KeyFeatures"]/div[@class="SecondaryBenefits"]/div[@class="sb"]' ) full = [] for i in range(len(tiles)): content = tiles[i].text.split('\n') header = f'<div class="two-col-asymmetrically"><div class="right-side"><h2 class="important-header">{content[0]}</h2>' description = f'<p style="font-size: large; text-align: center;">{content[1]}</p></div>' link = driver.find_element_by_xpath( f'//div[@id="KeyFeatures"]/div[@class="SecondaryBenefits"]/div[@class="sb"][{i + 1}]//img' ).get_attribute('src') if 'gif' in link: link = 'random ' full.append([header, description, link]) for i in range(len(full)): # obetnij link i rozszerzenie (do znalezienia nazwy obrazka do pobrania) full[i][2] = full[i][2][full[i][2].rfind('/') + 1:] full[i][2] = full[i][2][:full[i][2].find('.')] driver.find_element_by_xpath('//a[contains(text(), "Medias")]').click() driver.switch_to.window(driver.window_handles[2]) # zbierz wszystkie nazwy plików rows = driver.find_elements_by_xpath('//li') rows_links = driver.find_elements_by_xpath('//li/a') rows_text = [row.text for row in rows] # podczas wybierania losowego obrazka łatwiej znaleźć ładny od końca rows_text = rows_text[::-1] # oddziel obrazki znalezione, aby ich nie powielać used = [x[2] for x in full if x[2] != 'random'] for prod_img_name in prod_img_names: used.append(prod_img_name) for i in range(len(full)): img_link = False for row_t in rows_text: link_to_image = rows_links[rows_text.index(row_t) * (-1) - 1].get_attribute('href') name_of_image = row_t[row_t.find(':') + 2:row_t.find('.')] # Odrzuć inne możliwości niż "700x700 Presentation" if '700x700 Presentation' not in row_t: continue # odrzucaj rozszerzenia "bmp" oraz "eps" excension = link_to_image[-5:].lower() if 'bmp' in excension or 'eps' in excension: continue # sprawdź czy znaleziony wiersz należy do tych z opisu graficznego # Jeżeli nie został ustlaony, to wybierz pierwsz-lepszy if full[i][2] == 'random': if name_of_image not in used: img_link = link_to_image print( f'Wybrano: {row_t} w miejce Random ({name_of_image})') break elif full[i][2] in row_t: img_link = link_to_image print( f'Wybrano: {row_t} w miejce {full[i][2]} ({name_of_image})' ) break # na wszelki wypadek jeżeli nie znajdzie poszukiwanego zdjęcia, to wybierze losowe if not img_link and name_of_image not in used: print( f'Wybrano: {row_t} w miejce obrazka produktu ({name_of_image})' ) img_link = link_to_image # jak już tu jesteś, to pobierz też obrazki produktu for prod_img_name in prod_img_names: if prod_img_name in row_t: prod_img_link = link_to_image ImgRefractor.prod_img(f'{file_path}/{model}', prod_img_link, i) break if not img_link: print(f'Nie znaleziono obrazka dla: {full[i][2]}') continue print(img_link) # dodaj nazwę pliku do listy użytych ("used") if name_of_image not in used: used.append(name_of_image) size, file_type = ImgRefractor.desc_img(f'{file_path}/{model}', img_link, i) full[i][ 2] = f'<img src="https://matrixmedia.pl/media/wysiwyg/Electrolux/{model}/{i}.{file_type}" /></div>' print(used) driver.switch_to.window(driver.window_handles[1]) """ Opis krótki """ try: # weź do 8 elementów z opisu funkcji. Pierwszy element to słowo "funkcje" short = driver.find_element_by_id('FeaturesBullet').text.split( '\n')[1:9] for i in range(len(short)): while True: if short[i][0] == ' ': short[i] = short[i][1:] else: # dodaj tagi HTML'a short[i] = f'<li>{short[i]}<li>' break short = '<ul>' + ''.join(short) + '</ul>' except NoSuchElementException: short = 'Popraw' print('Coś jest nie tak ze ścieżką w Selenium do krótkiego opisu') """ Opis techniczny """ tech = [ '<table id="plan_b" class="data-table"><tbody><tr class="specs_category"><td colspan="2">Specyfikacja</td></tr>' ] for row in driver.find_elements_by_xpath( '//div[@id="TechnicalSpecs"]//div[@class="element"]'): name, value = row.text.split('\n') tech.append( f'<tr><td class="c_left">{name}</td><td class="c_left">{value}</td></tr>' ) tech.append('</tbody></table>') full = [['<div class="product-description-section">'] ] + full + [['</div>']] reg = [] for ele in full: reg.append(''.join(ele)) reg = ''.join(reg) tech = ''.join(tech) return [reg, short, tech]