我做了一個代碼來按數字搜索數字,如果數字是我想要的,則執行一些操作,代碼有效,但我試圖用更好的方式選擇這個數字:
# -*- 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')
uj5u.com熱心網友回復:
對于每個網頁元素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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322595.html
上一篇:Facebook:GraphAPI無法獲取為特定頁面創建的潛在客戶生成表單
下一篇:webdriver錯誤“urllib3.exceptions.ProxySchemeUnknown:代理URL沒有方案,應該以http://或https://開頭”
