如何使用 Selenium 获取属性值 - Python
How to get attribute value using Selenium - Python
HTML:
<div class data="#results">
<a href="javascript:void(0);" title="testtest" class="btn btn-white btn-sm btn-rounded dropdown-toggle">results</a>
如何使用 selenium 提取 title
的值?
这是我迄今为止尝试过的方法,但它给出了 None。
driver.get(url)
toogle = driver.find_element_by_xpath("XPATH_HERE")
val = toogle.get_attribute("title")
print(val)
要打印 title
属性的值,您必须引入 for the and you can use either of the following :
使用CSS_SELECTOR和class属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data='#results'] > a.btn.btn-white.btn-sm.btn-rounded.dropdown-toggle"))).get_attribute("title"))
使用 XPATH 和 innerText:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data='#results']/a[text()='results']"))).get_attribute("title"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
HTML:
<div class data="#results">
<a href="javascript:void(0);" title="testtest" class="btn btn-white btn-sm btn-rounded dropdown-toggle">results</a>
如何使用 selenium 提取 title
的值?
这是我迄今为止尝试过的方法,但它给出了 None。
driver.get(url)
toogle = driver.find_element_by_xpath("XPATH_HERE")
val = toogle.get_attribute("title")
print(val)
要打印 title
属性的值,您必须引入
使用CSS_SELECTOR和class属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data='#results'] > a.btn.btn-white.btn-sm.btn-rounded.dropdown-toggle"))).get_attribute("title"))
使用 XPATH 和 innerText:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data='#results']/a[text()='results']"))).get_attribute("title"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC