Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98800

How to deal with load more button?

$
0
0

I have this question about load more button. The page need to make this "road map": go to search's page, "see" product one by one, click on them and then go back to the search's page. My approach was to click on "load more" button until it doesn't exist anymore, then after scrapt it. But when goes to drive.back(), it's not the same page anymore and the automatic scroll that I did got lost, it doesn't render all the products at all. Also, with manual approach, I saw that if I, for example, click on load more button, select one product of this "other page", click on it, then when I go back to the search's page it doesn't go back to the previous page and I need to click on "load more" button once again. One approach that I thought was to get it with JSON, but the problem is: in my code the user will customize the search, so it will be different pages, never the same. So the url pointing to json would always change. How to make it? Anyway, here is the code:

def extrai_dados_prodirectsports():    print("entrou")    url_linha = "https://www.prodirectsport.com/soccer/l/adults/departments-boots/activity-football/brand-adidas/silo-predator/"    data_planilha = []    data_pdf = []    tamanhos = []    cotacao_libra()  # Supondo que essa função já esteja definida    i = 0    driver.get(url_linha)    #Cookies    WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH,"//button[@title='Accept all cookies']"))).click()    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[@id='zonos']")))    botao_close = driver.find_element(By.XPATH, "//a[@class = 'z-close']")    driver.execute_script("arguments[0].click();",botao_close)    #WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='global-popup']")))    botao = driver.find_element(By.XPATH,"//button[@aria-label='Close']")    driver.execute_script("arguments[0].click();",botao)    #driver.implicitly_wait(500000)    df_planilha = pd.DataFrame(columns=COLUNAS_PLANILHA)    #pdf = FPDF()    #pdf.add_page()    #pdf.set_font('Arial', 'B', 12)    x = 0     driver.maximize_window()    produtos_lista = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class = 'product-listing__grid']//div[@class = '_root_129ai_6 product-listing__grid-item']/a")))    produtos_container = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class = 'product-listing__grid']")))#driver.execute_script("arguments[0].scrollIntoView();", produtos_lista)    while True:        try:        # Reencontrar o botão dentro do loop para evitar stale element            botao_vermais = WebDriverWait(driver, 20).until(                    EC.presence_of_element_located((By.XPATH, "//div[@class='product-listing__view-more']/button"))                    )            if botao_vermais.is_displayed():                driver.execute_script("arguments[0].scrollIntoView();", botao_vermais)                driver.execute_script("arguments[0].click();", botao_vermais)                x += 1                print(x)            else:                driver.execute_script("arguments[0].scrollIntoView();", produto_container)                break  # Sai do loop se o botão não for mais exibido        except StaleElementReferenceException:            print("Elemento 'Ver mais' tornou-se stale, reencontrando o botão...")            continue  # Tenta novamente o loop        except TimeoutException:            print("Tempo esgotado esperando o botão 'Ver mais'.")            break    pagina_atual = driver.current_url    for index, value in enumerate(produtos_lista):        driver.get(pagina_atual)        produtos_lista = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class = 'product-listing__grid']//div[@class = '_root_129ai_6 product-listing__grid-item']/a")))            driver.execute_script("arguments[0].scrollIntoView();", produtos_lista[index])        time.sleep(10)        driver.save_screenshot("janela.png")        imagem_element = WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.TAG_NAME, "img")))        imagem = imagem_element.get_attribute("src")        i += 1        print(i)        #actions.move_to_element(produtos_lista[index]).perform()        driver.execute_script("arguments[0].click();", produtos_lista[index])        time.sleep(10)        driver.save_screenshot("projetoecommerce/screenshots_produtos/prodirectsports/" + str(i) +".png")        nome_produto = WebDriverWait(driver, 1000).until(EC.visibility_of_element_located((By.CLASS_NAME, "ml-meta__title"))).text        print(nome_produto)        preco = WebDriverWait(driver, 1000).until(EC.visibility_of_element_located((By.CLASS_NAME, "ml-prices__price"))).text        preco = re.sub("£", "", preco)        preco_produto = float(preco)         print(preco_produto)        codigo_produto = driver.current_url.split('-')[-1]        print(codigo_produto)        lista_tamanhos = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME,  "ml-size__sizes")))        if lista_tamanhos:            driver.execute_script("arguments[0].scrollIntoView();", lista_tamanhos)            for tamanho in lista_tamanhos.find_elements(By.XPATH, "//button[@class='ml-size__size qa-size-item']"):                tamanho_produto = tamanho.text                tamanhos.append(tamanho_produto)                if "-" in tamanho.text:                    tamanho_produto = tamanho.text.split('-')[0]                     tamanhos.append(tamanho_produto)                elif "(" in tamanho.text:                    tamanho_produto = tamanho.text.split('(')[0]                    tamanhos.append(tamanho_produto)                else:                    tamanho_produto = ""                    tamanhos.append(tamanho_produto)            print(tamanhos)        preco_reais = calcula_preco_reais(preco_produto)  # Função precisa estar definida        preco_venda = calcula_preco_venda(preco_produto)  # Função precisa estar definida        data_planilha.append({'Photo': imagem,'Código': codigo_produto,'Descrição': nome_produto,'Compra': preco_reais,'Venda': preco_venda,'Tamanhos': tamanhos            })        driver.back()           #driver.execute_script("window.history.go(-1)")        time.sleep(5)            #driv    figura = gera_pdf(data_pdf)    df_planilha = pd.DataFrame(data_planilha)    df_planilha.to_excel(url_linha +"catalogo.xlsx")    

What would be other approach for it? If I go product by product, then click on load more when I go back it would still if I didn't click on it at all when I go back to the search's page. It seems to me that json scraping would be the best approach, but how can I get the exact url for it? actually, url_line is a parameter of the method, the progra, will read it from a txt file. I got it out for you to test it.


Viewing all articles
Browse latest Browse all 98800


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>