使用 python 从伪元素获取值

Get value from pseudo element with python

我想从您在下图中看到的伪元素中获取价格值。如果我只将鼠标悬停在鼠标上,那么我可以看到一些值,比如它的价格(这就是我需要的)。我找到了“move_to_element”,所以现在我可以将鼠标悬停在程序上,但我仍然无法从元素中获取价格。 问题是即使我将鼠标悬停在检查器选项卡中也看不到打开的元素。

谢谢!

这是我想从中提取 :before 元素的代码:

<div onclick="Game.UpgradesById[503].click(event);" class="crate upgrade enabled" onmouseout="Game.setOnCrate(0);Game.tooltip.shouldHide=1;" onmouseover="if (!Game.mouseDown) {Game.setOnCrate(this);Game.tooltip.dynamic=1;Game.tooltip.draw(this,function(){return function(){return Game.crateTooltip(Game.UpgradesById[503],'store');}();},'store');Game.tooltip.wobble();}" id="upgrade0" style="background-position:-1056px -1296px;"></div>

在此之后有一个 en 元素 ::before 和 div

的结尾

当我手动访问该网站时,我得到的有点像您显示的内容,但是当我通过自动化 运行 时,它没有显示任何升级。因此,我不得不坚持检查身份(如 Grandma、Cursor 等);但他们没有在自动版本中显示。只能看到用户 ???。 Snapshot

最后,我只用 ??? 试了一下。我尝试了一种不同的方法,而不是使用 ::before::after,我试图查看某处是否存在任何工具提示,它确实存在。唯一需要注意的是,我能够从悬停中获取所有文本(我们必须再次通过代码从中提取所需的内容)。在这种情况下,这里是代码:

driver.get("https://orteil.dashnet.org/cookieclicker/")
time.sleep(10)
ActionChains(driver).move_to_element(driver.find_element(By.ID, 'product0')).perform()
time.sleep(1)
x = driver.find_element(By.ID, 'tooltip').text
print(x)

这是输出:

15
???
[owned : 0]

Process finished with exit code 0

请将元素更改为您想要的元素,您应该会得到结果。

关于如何仅提取值的查询:

将输出 x 与下一行的分隔符分开:\n 然后获取它的第一个索引。那会给你价值。下面是几行。

spl = int(x.split("\n")[0])
print(f'value: {spl}')
print(type(spl))

输出:

15
???
[owned : 0]
value: 15 # this line is fetched using the above code in this answer. It fetches the value from the `x`
<class 'int'>