无法获取内部标记- Python Selenium

qni6mghb  于 2023-03-28  发布在  Python
关注(0)|答案(1)|浏览(103)

我尝试在以下结构中获取文本“超过98(30天平均)”:

<div class="estimator-container">
<div> 
<span>Estimate sales per day for the rank</span> 
<input type="text" class="estimator-input">
<input type="button" value="Go" class="estimator-submit"> 
<span class="estimator-result">
<div>More than 98 (30 Days Avg)</div>
<div>More than 24 (Last Day)</div>

参考网址为https://www.fbatoolkit.com/
我的代码当前如下所示
driver.find_elements(By.XPATH, '//span[@Class = "estimator-result"]')
.get_attribute("outerHTML")函数确认我选择了正确的元素,但在语法中添加/div将返回null,.get_attribute("innerHTML")将返回空白文本。
谢谢

baubqpgj

baubqpgj1#

你可以使用一个规则的表达式来匹配这个模式。
假设您要提取数字,请使用以下模式:
More than (\d+) \(30 Days Avg\)see here了解详情)
下面是如何使用它:

import re
str_in = """<div class="estimator-container">
<div> 
<span>Estimate sales per day for the rank</span> 
<input type="text" class="estimator-input">
<input type="button" value="Go" class="estimator-submit"> 
<span class="estimator-result">
<div>More than 98 (30 Days Avg)</div>
<div>More than 24 (Last Day)</div>
"""

pat = r"More than (\d+) \(30 Days Avg\)"
res = re.findall(pat,str_in)

print(res[0])

其中打印:98

相关问题