How to capture data-tooltip using Selenium in Python

Asked

Viewed 65 times

-2

Hello, is the following my problem: I am making a web scraping in the following URL > https://www.maze.com.br/produto/tenis-nike-air-jordan-ma2-ultramarine-branco/4749595. I came across a slight problem when selecting the size of the product (39.5; 40; 41; etc.):

tamanho = input('Tamanho do tênis? ')

After that I would make one find_elements_by_class_name and see which class has the variable value tamanho.
Turns out this didn’t work out...
For some reason I’m getting an empty array []. After the error I thought of taking the amount that is in data-tooltip and compare if the value matches the initial variable tamanho, there’s the whole point! I don’t know how I can catch the value described in data-tooltip to make the comparison of values. I have already researched on the subject and ended up finding nothing and so I am here asking this question.
If also you know any other method for me to be able to select the size of the tennis shoes.

1 answer

0


I’m new to python, but from what I understand it’s more or less your idea ? Any questions with the code just ask, I left a while in the code so you can test it by going through the sizes typed in the input

from selenium import webdriver
from time import sleep

url = 'https://www.maze.com.br/produto/tenis-nike-air-jordan-ma2-ultramarine-branco/4749595'
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path='chromedriver.exe', options=options)

try:
    driver.get(url)
    sleep(2)
    xpath = driver.find_element_by_xpath('//div[@class="references"]').find_elements_by_xpath(
        '//button[@class="ui basic button"]')
    tamanhos = [tamanho.text for tamanho in xpath if tamanho.text != '']
    print(f"Os tamanhos disponíveis são: {tamanhos} ")
    print(len(tamanhos))
    while True:
        escolha = input("Escolha o tamanho que deseja: ")
        if escolha not in tamanhos:
            print("O tamanho não existe na lista")
        else:
            opcao = driver.find_element_by_xpath('//div[@class="references"]').find_element_by_xpath(
                f'//button[@data-tooltip="{escolha}"]')
            opcao.click()
except Exception as e:
    print(e)
finally:
    driver.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.