Contenido del curso
Contenido del curso
Arnaldo Martinez
Jonathan Mardones Guzmán
Santiago Herrera Velásquez
Sebastian Granda Gallego
Fredy Mendoza Vargas
Andres Felipe Barrera Pulido
Sandra Milena Rojas Herrán
Adrian Garcia
Orazio Cappadonna Cantor
Jason Mateo Pineda Lopera
Jason Mateo Pineda Lopera
Sebastián Andrade
Korpi delfin
Sebastián Andrade
Ignacio Solís Aguiluz
DAVID EDUARDO BAEZ SANCHEZ
Royer Guerrero Pinilla
Héctor Daniel Vega Quiñones
Santiago Herrera Velásquez
Hugo Gomez Tinoco
Ismael Danilo Herrera Sánchez
Christian Leonardo Hernandez Estrada
Junior Erny Aguayo Cuadros
Héctor Daniel Vega Quiñones
Steven Moreno
Héctor Daniel Vega Quiñones
José Manuel Piña Rodríguez
Jesús Alfredo González Monroy
Hugo Gomez Tinoco
Juan Sebastían Del Mar Aragon Rios
Alberto Fleitas
Esteban parra
Israel Yance
Héctor Daniel Vega Quiñones
Sebastián Andrés Sanhueza Tapia
Jose Luis
Francisco Peñalo Feliz
Christian Andrés Espinosa Villagómez
Para utilizar location, condition y higher_price cambie el código. El mensaje de Selenium fue "Element click intercepted".
# Anterior location.click() # Nuevo driver.execute_script("arguments[0].click();", location)
Con eso logre realizar el test.
Tuve el mismo problema, y me sirvió tu solución. ¡Muchas gracias!
Me paso lo mismo, que raro GRACIAS!
Este es mi implementación antes de ver el vídeo, mi intención era generalizarlo lo mas posible para cualquier variación como país, búsqueda, filtro, localidad y orden.
mercado_libre_page.py
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class MercadoLibrePage(object): def __init__(self, driver): self._driver = driver self._url = 'http://mercadolibre.com/' @property def is_loaded(self): WebDriverWait(self._driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, 'ml-site-list')) or EC.presence_of_element_located((By.NAME, 'as_word')) ) return True @property def keyword(self): input_field = self._driver.find_element_by_name('as_word') return input_field.get_attribute('value') def open(self): self._driver.get(self._url) def type_search(self, keyword): input_field = self._driver.find_element_by_name('as_word') input_field.send_keys(keyword) def click_submit(self): input_field = self._driver.find_element_by_name('as_word') input_field.submit() def search(self, keyword): self.type_search(keyword) self.click_submit() def order_data(self, elements): obj = {} for element in elements: obj[element.text.lower()] = element return obj def get_contries(self): contries = self._driver.find_elements_by_class_name('ml-site-link') return self.order_data(contries) def choise_contry(self, contry): contry_item = self.get_contries()[contry.lower()] contry_item.click() def get_filters(self): filters = WebDriverWait(self._driver, 10).until(EC.visibility_of_any_elements_located((By.CLASS_NAME, 'ui-search-filter-name'))) return self.order_data(filters) def choise_filter(self, keyword): self.get_filters()[keyword.lower()].click() def get_orders(self): list_buttom = WebDriverWait(self._driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'andes-dropdown__trigger'))) list_buttom.click() orders = self._driver.find_elements_by_class_name('andes-list__item-primary') return self.order_data(orders) def choise_order_by(self, keyword): self.get_orders()[keyword.lower()].click() def get_top_5_elements_result(self): WebDriverWait(self._driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'ui-search-layout__item'))) data = [[None, None]] * 5 for i in range(5): data[i][0] = self._driver.find_element_by_xpath(f'//*[@id="root-app"]/div/div/section/ol/li[{i+1}]/div/div/div[2]/div[1]/a/h2').text data[i][1] = self._driver.find_element_by_xpath(f'//*[@id="root-app"]/div/div/section/ol/li[{i+1}]/div/div/div[2]/div[2]/div/div/span[1]/span[2]').text return data
test_mercado_libre.py
import unittest from selenium import webdriver from mercado_libre_page import MercadoLibrePage class CompareProducts(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome(executable_path = r'./chromedriver.exe') cls.driver.maximize_window() def test_mercado_libre(self): mcl = MercadoLibrePage(self.driver) mcl.open() self.assertTrue(mcl.is_loaded) mcl.choise_contry('colombia') mcl.search('playstation 4') mcl.choise_filter('nuevo') mcl.choise_filter('Bogotá D.C.') mcl.choise_order_by('Mayor precio') print(mcl.get_top_5_elements_result()) @classmethod def tearDownClass(cls): cls.driver.close() if __name__ == "__main__": unittest.main(verbosity=2)
Muchas gracias!
Uffff vacanismo gracias!!!
Mucho cuidado con el botón de cookies. Como bien es sabido, el chromedriver ejecuta en incógnito. Cuando tú estás revisando mucho ++ojo++, porque cuando exploras, lo haces hasta con tu cuenta ya ingresada, y claro, con las cookies ya guardadas. Pero es muy diferente, cuando ingresas en incógnito, porque allí te pide que o aceptes o rechaces las cookies, por ser la "primera vez" que ingresas.
Para ello, debes poner este código dentro del ejercicio de la clase:
Después de buscar el playstation y antes de hacer el filtro de ubicación:
cookies_in = driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/button[1]") cookies_in.click() sleep(3)
Y eso es todo, te funcionará perfecto.
++Consejo: ++ Al hacer un script de automatización y que vayas haciendo la exploración de la página web, realiza la exploración en incógnito, pues así garantizas el comportamiento normal que se tendría con el chromedriver, que es una especie de incógnito.
Espero les sirva...
en mi caso lo use antes de buscar playstation despues no me funciona
Marzo-2021. Sale este error: Message: Element <span class="ui-search-filter-name"> is not clickable at point (113,643) because another element <div id="cookieDisclaimerBanner" class="nav-cookie-disclaimer"> obscures it. Parece que usaron alguna cookie para bloquear busquedas automatizadas con Selenium (por ejemplo), F. De todas maneras gran curso
No es problema, debes usar la solucion mencionada en los anteriores post:
location=driver.find_element_by_partial_link_text('Bogotá D.C.') driver.execute_script("arguments[0].click();", location)
<code>
<code>
En caso de dudas te dejo el código a hoy 30 de marzo 2021 100% funcional
import unittest from selenium import webdriver from time import sleep class Typos(unittest.TestCase): def setUp(self): self.driver=webdriver.Chrome(executable_path = './chromedriver_win32/chromedriver.exe') driver=self.driver driver.maximize_window() driver.get('https://www.mercadolibre.com') def test_search_ps4(self): driver=self.driver country=driver.find_element_by_id('CO') country.click() sleep(3) search_field=driver.find_element_by_name('as_word') search_field.click() search_field.clear() search_field.send_keys('playstation 4') search_field.submit() sleep(3) location=driver.find_element_by_partial_link_text('Bogotá D.C.') driver.execute_script("arguments[0].click();", location) sleep(3) condition=driver.find_element_by_partial_link_text('Nuevo') driver.execute_script("arguments[0].click();", condition) sleep(3) order_menu=driver.find_element_by_css_selector('#root-app > div > div > section > div.ui-search-view-options__container > div > div > div.ui-search-view-options__group > div.ui-search-sort-filter > div > div > button') order_menu.click() value=driver.find_element_by_partial_link_text('Mayor precio') driver.execute_script("arguments[0].click();", value) sleep(3) articles=[] prices=[] for i in range(5): article_name=driver.find_element_by_xpath(f'/html/body/main/div/div/section/ol/li[{i+1}]/div/div/div[2]/div[1]/a/h2').text articles.append(article_name) article_price=driver.find_element_by_xpath(f'/html/body/main/div/div/section/ol/li[{i+1}]/div/div/div[2]/div[2]/div[1]/div[1]/div/div/span[1]/span[2]').text prices.append(article_price) print(articles, prices) def tearDown(self): self.driver.close() if __name__=="__main__": unittest.main(verbosity=2)
Si es legal Automatizar mercadolibre? solo por las dudas xd
Es legal :D
Gracias, es que no habia entendido el archivo
Este es mi códgio por si le sirve a alguien. Luego de unos arreglos que no me funcionaban algunos clicks y encontre la solución en los comentarios.
import unittest from selenium import webdriver from time import sleep class TestingMercadoLibre(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(executable_path = r'./chromedriver.exe') driver = self.driver driver.maximize_window() driver.get('https://www.mercadolibre.com') def test_search_PS4(self): driver = self.driver country = driver.find_element_by_id("CO") country.click() search_field = driver.find_element_by_name("as_word") search_field.click() search_field.clear() search_field.send_keys('playstation 4') search_field.submit() sleep(3) location = driver.find_element_by_xpath("//div/div/aside/section[2]/dl[9]/dd[1]/a") driver.execute_script("arguments[0].click();", location) sleep(3) condition = driver.find_element_by_xpath("//div/div/aside/section[3]/dl[6]/dd[1]/a") driver.execute_script("arguments[0].click();", condition) sleep(3) order_menu = driver.find_element_by_class_name("andes-dropdown__trigger") order_menu.click() higher_price = driver.find_element_by_xpath("//div/div/aside/section[2]/div[2]/div[1]/div/div/div/ul/li[3]/a") driver.execute_script("arguments[0].click();", higher_price) sleep(3) articles = [] prices = [] for i in range(5): article_name = driver.find_element_by_xpath(f'/html/body/main/div/div/section/ol/li[{i + 1}]/div/div/div[2]/div[1]/a').text articles.append(article_name) article_price = driver.find_element_by_xpath(f'/html/body/main/div/div/section/ol/li[{i + 1}]/div/div/div[2]/div[2]/div[1]/div[1]/div/div/span[1]/span[2]').text prices.append(article_price) print(articles, prices) def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()
Me toco modificar algunos click
import unittest from selenium import webdriver from api_data_mock import ApiDataMock from selenium.webdriver.support.ui import Select # Modulo para poder seleccionar del dropdown from time import sleep from selenium.webdriver.common.by import By #Hacer referencia a un elemento del sitio web, para interactuar from selenium.webdriver.support.ui import WebDriverWait # Modulo para manejo de esperas explicitas from selenium.webdriver.support import expected_conditions as EC #esperas explicitas class TestingMercadoLibre(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(executable_path = r'./chromedriver.exe') driver = self.driver driver.maximize_window() driver.get('**********') # adiconar la pagina def test_search_xbox(self): driver = self.driver country = driver.find_element_by_id('CO') country.click() sleep(2) search_field = driver.find_element_by_name('as_word') search_field.clear() search_field.send_keys('xbox') search_field.submit() sleep(2) location = driver.find_element_by_xpath('/html/body/main/div/div/aside/section[2]/dl[8]/dd[1]/a/span[1]') driver.execute_script("arguments[0].click();", location) # location.click() sleep(2) condition = driver.find_element_by_partial_link_text('Nuevo') driver.execute_script("arguments[0].click();", condition) # condition.click() sleep(2) order_menu = driver.find_element_by_class_name('andes-dropdown__trigger') order_menu.click() higher_price = driver.find_element_by_css_selector('li.andes-list__item:nth-child(3) > div:nth-child(1) > div:nth-child(1) > a:nth-child(1)') higher_price.click() sleep(2) articles = [] prices = [] for i in range(5): article_name = driver.find_element_by_xpath(f'/html/body/main/div/div/section/ol/li[{i + 1}]/div/div/div[2]/div[1]/a/h2').text price_article = driver.find_element_by_xpath(f'/html/body/main/div/div/section/ol/li[{i + 1}]/div/div/div[2]/div[2]/div/div/span[1]/span[2]').text articles.append(article_name) prices.append(price_article) print(articles,prices) def tearDown(self): self.driver.implicitly_wait(5) self.driver.close() if __name__ == '__main__': unittest.main(verbosity=2)
Creo que es mejor usar un diccionario en vez de dos listas
products = {} for i in range(5): article_name = driver.find_element_by_xpath(f'/html/body/main/div/div/section/ol/li[{i + 1}]/div/div[2]/div/h2/a/span').text price = driver.find_element_by_xpath(f'/html/body/main/div/div/section/ol/li[{i + 1}]/div/div[2]/div/div[1]/div/span[2]').text products[article_name] = price
Totalmente. La idea de estos retos es que también vean cómo pueden optimizar su código ;)
QUE BIEN! yo lo hice con tuplas y va perfecto tambien
for i in range(Q_ARTICLES): title = driver.find_element_by_xpath(f'//*[@id="root-app"]/div/div/section/ol/li[{i + 1}]/div/div/div[2]/div[1]/a/h2') price = driver.find_element_by_xpath(f'//*[@id="root-app"]/div/div/section/ol/li[{i + 1}]/div/div/div[2]/div[2]/div[1]/div[1]/div/div/span[1]') product = (title.text, price.text) products.append(product) print(products)```
Con la ayuda de los comentarios y unas modificaciones más porque el sitio cambia constantemente aquí el código funcional para la fecha 09-10-2021
import unittest from pyunitreport import HTMLTestRunner from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from time import sleep class MercadoLibre(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver') driver = cls.driver driver.get('https://www.mercadolibre.com/') driver.maximize_window() def test_search_ps4(self): driver = self.driver choose_country = driver.find_element_by_id('CO') choose_country.click() search_bar = driver.find_element_by_class_name('nav-search-input') search_bar.click() search_bar.clear() search_bar.send_keys('playstation 4') search_bar.submit() driver.implicitly_wait(3) pick_news = driver.find_element_by_partial_link_text('Nuevo') driver.execute_script("arguments[0].click();", pick_news) driver.implicitly_wait(3) pick_location = driver.find_element_by_partial_link_text('Bogotá D.C.') driver.execute_script("arguments[0].click();", pick_location) driver.implicitly_wait(3) pick = driver.find_element_by_class_name('andes-dropdown__trigger') driver.execute_script("arguments[0].click();", pick) driver.implicitly_wait(10) pick_expensive_price = driver.find_elements_by_class_name('andes-list__item-primary') pick_expensive_price[2].click() articles = [] prices = [] for i in range(5): article_name = driver.find_element_by_css_selector(f'#root-app > div > div > section > ol > li:nth-child({i+1}) > div > div > div.ui-search-result__content-wrapper > div.ui-search-item__group.ui-search-item__group--title > a > h2').text articles.append(article_name) article_price = driver.find_element_by_css_selector(f'#root-app > div > div > section > ol > li:nth-child({i+1}) > div > div > div.ui-search-result__content-wrapper > div.ui-search-result__content-columns > div.ui-search-result__content-column.ui-search-result__content-column--left > div.ui-search-item__group.ui-search-item__group--price > a > div > div > span.price-tag.ui-search-price__part > span.price-tag-amount > span.price-tag-fraction').text prices.append(article_price) for x in range(len(articles)): print(prices[x]+' -> '+articles[x]) @classmethod def tearDownClass(cls): cls.driver.close() if __name__ == '__main__': unittest.main(verbosity=2, testRunner=HTMLTestRunner(output = 'reportes', report_name = 'ML_report'))
Gracias de nuevo Hugo 😅 Incluso volvió a cambiar.
Esta es mi implementacion
test_mercadolibre:
from selenium import webdriver import unittest from mercadolibre_page import MercadoLibrePage from time import sleep class testMercadoLibre(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome(executable_path=r'C:/chromedriver.exe') cls.driver.maximize_window() def test_search(self): mercadolibre = MercadoLibrePage(self.driver) mercadolibre.open() mercadolibre.select_country('Colombia') self.assertEqual(mercadolibre.current_url, 'https://www.mercadolibre.com.co/#from=homecom') mercadolibre.type_search('playstation 4') mercadolibre.click_submit() self.assertEqual('playstation 4', mercadolibre.keyword) mercadolibre.acept_cookies() mercadolibre.add_filter('Nuevo') self.assertEqual(mercadolibre.last_filter, 'Nuevo') mercadolibre.add_filter('Bogotá D.C.') self.assertEqual(mercadolibre.last_filter, 'Bogotá D.C.') mercadolibre.open_price_filter_window() mercadolibre.add_price_filter('Menor precio') self.assertEqual(mercadolibre.current_order, 'Menor precio') mercadolibre.get_information(60) @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == '__main__': unittest.main(verbosity=2)
mercadolibre_page:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException import csv import time class MercadoLibrePage(object): def __init__(self, driver): self._driver = driver self._url = 'https://www.mercadolibre.com' self._search_locator = 'as_word' self._price_filter_locator = '//div[@class="andes-widther"]//button[@class="andes-dropdown__trigger"]' self._filters = [] @property def is_loaded(self): WebDriverWait(self._driver, 10).until(EC.presence_of_element_located((By.NAME, self._search_locator))) return True @property def keyword(self): search_field = self._driver.find_element_by_name(self._search_locator) return search_field.get_attribute('value') @property def current_url(self): return self._driver.current_url @property def last_filter(self): return self._filters[len(self._filters)-1 ] @property def current_order(self): return WebDriverWait(self._driver, 10).until(EC.element_to_be_clickable((By.XPATH, self._price_filter_locator))).text def open(self): self._driver.get(self._url) def acept_cookies(self): try: location_window = self._driver.find_element_by_xpath('//div[@class="onboarding-cp"]') location_window.find_element_by_class_name('andes-tooltip-button-close').click() except NoSuchElementException: pass try: self._driver.find_element_by_id('cookieDisclaimerButton').click() except: pass def select_country(self, country): self._driver.find_element_by_link_text(country).click() def type_search(self, keyword): search_field = WebDriverWait(self._driver, 10).until(EC.presence_of_element_located((By.NAME, self._search_locator))) search_field.send_keys(keyword) def click_submit(self): search_field = self._driver.find_element_by_name(self._search_locator) search_field.submit() def add_filter(self, filter_keyword): WebDriverWait(self._driver, 10).until(EC.element_to_be_clickable((By.XPATH, f'//section[@class="ui-search-filter-groups"]//span[text()="{filter_keyword}"]'))).click() self._filters.append(filter_keyword) def open_price_filter_window(self): WebDriverWait(self._driver, 10).until(EC.element_to_be_clickable((By.XPATH, self._price_filter_locator))).click() #the option filter are: [Más relevantes, Menor precio, Mayor precio] def add_price_filter(self, filter_keyword): self._driver.find_element_by_xpath(f'//ul[@class="andes-list andes-list--size-compact andes-list--dropdown andes-list--selectable"]//div[text()="{filter_keyword}"]').click() def get_information(self, product_quantity): #get information of all products that you need products = self._driver.find_elements_by_class_name('ui-search-layout__item') data = [[] for i in range(0, 2)] print() if products: n_products = 0 i = 0 while True: try: name = products[i].find_element_by_class_name('ui-search-item__title') price = products[i].find_element_by_class_name('price-tag-fraction') data[0].append(name.text) data[1].append(price.text) print(f'name: {name.text} \nprice: {price.text}\n') i += 1 n_products += 1 if n_products == product_quantity: print(f'Se han encontraron los {n_products} productos.\n') break except IndexError: i = 0 try: self._driver.find_element_by_xpath('//div[@class="ui-search-pagination"]//li//span[text()="Siguiente"]').click() self._driver.implicitly_wait(10) products = self._driver.find_elements_by_class_name('ui-search-layout__item') except NoSuchElementException: print(f'Solo se encontraron {n_products} productos.\n') break self._save_data(data) else: print('\nNo se encontraron productos\n') def _save_data(self, data): #save data on csv file date = time.strftime('%d_%m_%Y', time.localtime()) filename = f'products_data_{date}.csv' headers = ['name', 'price'] with open(filename, mode= 'w+', encoding='utf-8', ) as f: writer = csv.writer(f) writer.writerow(headers) for i in range(0, len(data[0])): row = [data[0][i], data[1][i]] writer.writerow(row)
El class_name de la variable Order_menu hoy 26 julio es andes-dropdown__trigger y no ui-dropdown__link buen curso
Buena observación, recordemos que hay sitios que pueden cambiar constantemente.
Un selector de css más corto li.ui-list__item:nth-child(3) > a:nth-child(1) para la variable higher_price
Excelente alternativa, asi obtienes un código más prolijo ;)
Excelente profe, aplicar todo y muy util ejemplo.
Al terminar una prueba no quiero que el navegador se cierre como puedo hacer esto??
Al final de la prueba pon sleep(60) y te dará un minuto para que puedas comprobar que todo se aplicó correctamente Para agregar sleep solo importas el módulo time
from time import sleep
tengo entendido que el metodo "find_element_by" fue eliminado en selenium desde la version 4.3. Es posible trabajar hoy en dia con este curso?
Es totalmente posible solo debes cambiar tu código donde sea necesario, eso si, vas a tener que ir solucionando errores debido a la actualización, en esa función en específico te dejo la documentación: Link
es posible pasar el check de el recaptcha V2 y V3 de google usando selenium??
Es posible, pero no lo hagas a no ser que sea en tus peopios peoyectos 😉
El Xpath que menciona el profe, no me sirvió dado que cambia un poco la estructura de la lista de producto en la web de mercado libre, para eso hice un xpath más flexible, que captura los productos a pesar de los cambios en cada recarga
for i in range(8): article_name = driver.find_element(By.XPATH, f'//li[{i + 1}]//h2').text articles.append(article_name) article_price = driver.find_element(By.XPATH, f'//li[{i + 1}]//span[contains(@class, "andes-money-amount__fraction")]').text prices.append(article_price) for i in range(8): print(articles[i], "| Precio:", prices[i])
Así lo hice yo
from itertools import product import unittest from selenium import webdriver from time import sleep class PruebaTecnica(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(executable_path=r'E:\Descargas\chromedriver.exe') driver = self.driver driver.implicitly_wait(50) driver.maximize_window() #driver.get('https://www.mercadolibre.com') driver.get('https://www.google.com') def test_mercado_libre(self): input_field = self.driver.find_element_by_name('q') search_page = 'Mercado libre' input_field.send_keys(search_page) input_field.submit() find_option = self.driver.find_element_by_partial_link_text('Mercado Libre Colombia - Envíos Gratis en el día') find_option.click() #country = self.driver.find_element_by_id('CO') #country.click() #search_product = self.driver.find_element_by_class_name('nav-search-input') search_product = self.driver.find_element_by_name('as_word') name_product = 'Playstation 4' search_product.send_keys(name_product) search_product.submit() #condition = self.driver.find_element_by_css_selector('#root-app > div > div.ui-search-main.ui-search-main--exhibitor.ui-search-main--only-products > aside > section > div:nth-child(3) > ul > li:nth-child(1) > a > span.ui-search-filter-name') state_product = self.driver.find_element_by_partial_link_text('Nuevo') state_product.click() sleep(5) #city = self.driver.find_element_by_link_text('#root-app > div > div.ui-search-main.ui-search-main--exhibitor.ui-search-main--only-products > aside > section.ui-search-filter-groups > div:nth-child(3) > ul > li:nth-child(1) > a > span.ui-search-filter-name') city = self.driver.find_element_by_partial_link_text('Bogotá D.C.') city.click() sleep(5) order_menu = self.driver.find_element_by_class_name('andes-dropdown__display-values') order_menu.click() sleep(5) order_price = self.driver.find_element_by_css_selector('#andes-dropdown-más-relevantes-list-option-price_desc > div > div > span') order_price.click() sleep(5) #name_article = self.driver.find_element_by_class_name('ui-search-item__title') #name_article.click() #vamos a colocar los productos dentro de una lista articles = [] prices = [] for i in range(5): article_name = self.driver.find_element_by_xpath(f'//*[@id="root-app"]/div/div[2]/section/ol/li[{i+1}]/div/div/div[2]/div[1]/a/h2').text articles.append(article_name) article_price = self.driver.find_element_by_xpath(f'//*[@id="root-app"]/div/div[2]/section/ol/li[{i+1}]/div/div/div[2]/div[2]/div[1]/div[1]/div/div/div/span[1]/span[2]/span[2]').text prices.append(article_price) print(articles, prices) def tearDown(self): self.driver.close() if __name__ == '__main__': unittest.main(verbosity=2)
Realice la recolección de todos los datos mendiante CSS_SELECTOR y converti la data en un diccionario con los artículos y sus precios.
def test_search_ps4(self): driver = self.driver country = driver.find_element(By.ID, 'CO') country.click() accept_cookie_button = driver.find_element( By.XPATH, '/html/body/div[2]/div[1]/div[2]/button[1]') accept_cookie_button.click() sleep(2) search_input = driver.find_element(By.NAME, 'as_word') search_input.click() search_input.clear() search_input.send_keys('playstation 4') search_input.submit() sleep(2) location = driver.find_element(By.PARTIAL_LINK_TEXT, 'Bogotá D.C.') location.click() sleep(2) condition = driver.find_element(By.PARTIAL_LINK_TEXT, 'Nuevo') condition.click() sleep(2) order_menu = driver.find_element( By.CLASS_NAME, 'andes-dropdown__trigger') order_menu.click() higher_price = driver.find_element( By.CSS_SELECTOR, '#andes-dropdown-más-relevantes-list-option-price_desc') higher_price.click() article_elements = driver.find_elements( By.CSS_SELECTOR, 'div.ui-search-result__content-wrapper h2.ui-search-item__title') price_elements = driver.find_elements( By.CSS_SELECTOR, 'div.ui-search-result__content-wrapper div.ui-search-price--size-medium div.ui-search-price__second-line span.price-tag-fraction') item_count = len(article_elements) data = [{'article': article_elements[i].text, 'price': price_elements[i].text} for i in range(item_count)] print(f'\nFound {item_count} items.') print('\nData: ', data)
Es el segundo curso que veo de Hector Vega , y la verdad hay temas que quedan claros por que son sencillos , pero no porque explique bien , ; en general el no explica nada bien ,solo hace y hace código , da ganas de dormirse antes que ver el curso , no se sabe incluso ni que va hacer , pesimo curso. sobre 10 , le doy un 6/10