在Python中转换Selenium的输出

2024-09-27 23:24:51 发布

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

我有以下代码:

from selenium import webdriver

# Set url and path
url = 'https://osu.ppy.sh/beatmapsets?m=0'
driver = webdriver.Chrome(executable_path=r"C:\Users\Gabri\anaconda3\chromedriver.exe")
driver.get(url)

# Select the item that i want
try:
    dif = driver.find_element_by_css_selector('div.beatmapset-panel__beatmap-dot')
    dif.text
    print(dif)
except:
    print('not found')

我正在尝试选择此地图难度“紫色专家”->https://imgur.com/a/G224rka但是我无法继续我的代码,因为输出是"<selenium.webdriver.remote.webelement.WebElement (session="3cdaf38d0673d0aebe49733d629eae5c", element="60d6241b-80f7-42c8-bf38-9fd2c8574b08")>",我希望它是一个类似“expert”或--bg:var(--diff expert);“我如何翻译或转换?”?我确实尝试过用'[class*="beatmapset-panel__beatmap-dot"'进行选择,结果是一样的。有人能帮我吗


Tags: path代码fromurldriverseleniumelementdot
2条回答

要打印元素文本,需要按以下方式更改代码:

from selenium import webdriver

# Set url and path
url = 'https://osu.ppy.sh/beatmapsets?m=0'
driver = webdriver.Chrome(executable_path=r"C:\Users\Gabri\anaconda3\chromedriver.exe")
driver.get(url)

# Select the item that i want
try:
    dif = driver.find_element_by_css_selector('div.beatmapset-panel__beatmap-dot')    
    print(dif.text)
except:
    print('not found')

您需要将鼠标悬停在某个元素上,然后等待数据出现并获取其文本:

下面是列表中第一个游戏的片段。要获得所有游戏,您需要另一个循环

我使用ActionChains将鼠标悬停在元素上。即使对我来说,找到这个网站的定位器也不容易

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


driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get("https://osu.ppy.sh/beatmapsets?m=0")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".beatmapsets__items-row:nth-of-type(1)>.beatmapsets__item:nth-of-type(1)")))
games = driver.find_element_by_css_selector(".beatmapsets__items-row:nth-of-type(1) .beatmapsets__item:nth-of-type(1) .beatmapset-panel__info-row extra")
actions = ActionChains(driver)
actions.move_to_element(games).perform()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".beatmaps-popup__group")))
levels = driver.find_elements_by_css_selector(".beatmaps-popup__group .beatmaps-popup-item__col.beatmaps-popup-item__col name.u-ellipsis-overflow")
for level in levels:
    print(level.text)

输出:

Hinsvar's Hard
Zelqurre's Insane
Amamir's Shining Stars

要通过级别列表进行迭代,请使用此css选择器:

.beatmapsets__items-row:nth-of-type(1) .beatmapsets__item:nth-of-type(1) .beatmapset-panel__info-row extra

并迭代此定位器:

.beatmapsets\uuuu项目行:第n个类型(1)。beatmapsets\uuuu项目:第n个类型(1)。beatmapset-panel\uuu信息行额外

.beatmapsets\uuuu项目行:第n个类型(2)。beatmapsets\uuuu项目:第n个类型(1)。beatmapset-panel\uuu信息行额外

更新: 要获得分数,请使用:

scores= driver.find_elements_by_css_selector(".beatmaps-popup__group .beatmaps-popup-item__col.beatmaps-popup-item__col difficulty")
for score in scores:
    print(score.text)

输出将是:

2.58
3.46
4.55
4.90
5.97

另外,检查这个关于如何将结果放入一个列表的答案:Trouble retrieving elements and looping pages using next page button

最后在这里阅读有关css选择器的内容:https://www.w3schools.com/cssref/css_selectors.asp

我通常更喜欢用它们,因为它们比较短

相关问题 更多 >

    热门问题