Home > Net >  Selecting a variable property in Selenium with Python
Selecting a variable property in Selenium with Python

Time:10-19

I did a code to search number by number and do something if the number is what i want, the code works, but i trying to select this number with a better way:

# -*- encoding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait

# Chromedriver and url
driver = webdriver.Chrome(executable_path=r"C:\Users\Gabri\anaconda3\chromedriver.exe")
wait: WebDriverWait = WebDriverWait(driver, 20)
driver.get('https://osu.ppy.sh/beatmapsets/941078')

# The list of all numbers
maps = driver.find_elements_by_css_selector(""".beatmapset-beatmap-picker__beatmap""")

# Loop for clean the string and convert to float
for stars in maps:
    actions = ActionChains(driver)
    actions.move_to_element(stars).perform()

    # I found this way to clean and show, is a lot of steps ... but it works =/
    treatment = driver.find_element_by_css_selector('.beatmapset-header__star-difficulty').text
    treatment = treatment .replace('Star Difficulty ', '')
    treatment = treatment .replace(',', '.')
    clean = float(treatment )

    # This way this is much more efficient, but I can't make it work
    attempt = driver.find_element_by_css_selector(""".beatmap-icon""").get_property('data-stars')

    # In final, both numbers have to be the same (at least that's how it was supposed to happen)
    print(f'{attempt=}\n{clean=}\n')

I already tried attempt = driver.find_elements_by_css_selector(""".beatmap-icon""")[maps.index(stars)].get_property('data-stars') but returns None

The numbers that i trying to select is these: enter image description here

Basically, the idea is show each number that appear below:

difficulty number

But using just the attempt variable or something short like this

Someone can help me with this selector?

FINAL CODE:

# -*- encoding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome(executable_path=r"C:\Users\Gabri\anaconda3\chromedriver.exe")
wait: WebDriverWait = WebDriverWait(driver, 20)
driver.get('https://osu.ppy.sh/beatmapsets/941078')
maps = driver.find_elements_by_css_selector(""".beatmapset-beatmap-picker__beatmap""")

for stars in maps:
    actions = ActionChains(driver)
    actions.move_to_element(stars).perform()

    attempt2 = float(driver.find_element_by_xpath(f"(//div[contains(@class,'beatmap-icon')])[{maps.index(stars) 1}]").get_attribute('data-stars'))

    print(f'{attempt2=}\n')

CodePudding user response:

for each web element stars , you are just looking for first

attempt = driver.find_element_by_css_selector(""".beatmap-icon""").get_property('data-stars')

attribute. You will have to induce index for attempt as well.

Code :

# The list of all numbers
maps = driver.find_elements_by_css_selector(""".beatmapset-beatmap-picker__beatmap""")

j = 1
# Loop for clean the string and convert to float
for stars in maps:
    actions = ActionChains(driver)
    actions.move_to_element(stars).perform()

    # I found this way to clean and show, is a lot of steps ... but it works =/
    treatment = driver.find_element_by_css_selector('.beatmapset-header__star-difficulty').text
    treatment = treatment.replace('Star Difficulty ', '')
    treatment = treatment.replace(',', '.')
    try:
        clean = float(treatment )
    except:
        print('could not convert string to float:')
        pass

    # This way this is much more efficient, but I can't make it work
    attempt = driver.find_element_by_xpath(f"(//div[contains(@class,'beatmap-icon')])[{j}]").get_attribute('data-stars')
    j = j   1
    # In final, both numbers have to be the same (at least that's how it was supposed to happen)
    print(f'{attempt=}\n{clean=}\n')

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

Output :

attempt='4.96'
clean=4.96

attempt='6.03'
clean=6.03

attempt='6.07'
clean=6.07

attempt='6.33'
clean=6.33

attempt='6.39'
clean=6.39

attempt='6.52'
clean=6.52

attempt='7.14'
clean=7.14
  • Related