使用Python在Selenium中选择变量属性

2024-09-27 23:20:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个代码,按数字搜索数字,如果数字是我想要的,则执行一些操作,代码有效,但我尝试以更好的方式选择此数字:

# -*- 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')

我已经尝试了attempt = driver.find_elements_by_css_selector(""".beatmap-icon""")[maps.index(stars)].get_property('data-stars'),但返回了None

我试图选择的数字如下: enter image description here

基本上,这个想法是显示下面出现的每个数字:

difficulty number

但是只使用attempt变量或类似的简短语句

有人能帮我拿这个选择器吗

最终代码:

# -*- 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')

Tags: tofromimportgetbydriverselenium数字
1条回答
网友
1楼 · 发布于 2024-09-27 23:20:03

对于每个web元素stars,您只需要查找第一个

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

属性。您还必须为尝试引入索引

代码:

# 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')

导入:

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

输出:

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

相关问题 更多 >

    热门问题