Selenium python 通过 class 和标题获取属性
Selenium python get attribute by class and title
我用谷歌搜索了很多,但找不到答案,我正在尝试提取标题中的“90,856”。它的值随时间变化:
<div class="card hreddeep">
<div class="card-header hbuilt">
<div class="card-body">
<div class="row">
<div class="col-6 text-left">
<div class="h6 text-uppercase param-title">Search<br>Volume</div>
<div class="h4 param-content" title="90,856">
我的代码是:
find_element_by_xpath("//div[@class='h4-param-content']").get_attribute("title")
感谢您的帮助。谢谢
您的 XPath 包含错字(根据您的示例数据):h4-param
而不是 h4 param
...
首先,测试:
find_element_by_xpath("//div[@class='h4 param-content']").get_attribute("title")
如果它不起作用,请尝试使用预期条件。
添加以下导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
代码:
wait = WebDriverWait(driver, 10)
output = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='h4 param-content']"))).get_attribute("title")
print(output)
我用谷歌搜索了很多,但找不到答案,我正在尝试提取标题中的“90,856”。它的值随时间变化:
<div class="card hreddeep">
<div class="card-header hbuilt">
<div class="card-body">
<div class="row">
<div class="col-6 text-left">
<div class="h6 text-uppercase param-title">Search<br>Volume</div>
<div class="h4 param-content" title="90,856">
我的代码是:
find_element_by_xpath("//div[@class='h4-param-content']").get_attribute("title")
感谢您的帮助。谢谢
您的 XPath 包含错字(根据您的示例数据):h4-param
而不是 h4 param
...
首先,测试:
find_element_by_xpath("//div[@class='h4 param-content']").get_attribute("title")
如果它不起作用,请尝试使用预期条件。
添加以下导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
代码:
wait = WebDriverWait(driver, 10)
output = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='h4 param-content']"))).get_attribute("title")
print(output)