我做了一个代码,按数字搜索数字,如果数字是我想要的,则执行一些操作,代码有效,但我尝试以更好的方式选择此数字:
# -*- 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
基本上,这个想法是显示下面出现的每个数字:
但是只使用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')
对于每个web元素
stars
,您只需要查找第一个属性。您还必须为尝试引入索引
代码:
导入:
输出:
相关问题 更多 >
编程相关推荐