如何从 Python 中的 selenium 中提取标题值?
How to Extract Title value from selenium in Python?
我想通过 selenium 从中获取 title
值,但我无法做到这一点。需要专家意见。
items = driver.find_elements_by_tag_name("li")
for item in items:
followers = link.get_attribute('title')
print (followers)
它向我显示了一个空字符串。为什么会这样?
如果下面的xpath
//li//a[@href='/cristiano/followers']
在HTML-DOM
中是唯一的,那么你可以使用下面的代码:
wait = WebDriverWait(driver, 20)
try:
a_tag = wait.until(EC.visibility_of_element_located((By.XPATH, "//li//a[@href='/cristiano/followers']")))
print(a_tag.find_element(By.XPATH, ".//descendant::span").get_attribute('title'))
except:
print("something went wrong")
pass
这个用例实际上不需要循环。
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
检查元素是否唯一的步骤:
Press F12 in Chrome
-> 转到 element
部分 -> 执行 CTRL + F
-> 然后粘贴 xpath
并查看是否需要 element
正在 突出显示 与 1/1
匹配节点。
我想通过 selenium 从中获取 title
值,但我无法做到这一点。需要专家意见。
items = driver.find_elements_by_tag_name("li")
for item in items:
followers = link.get_attribute('title')
print (followers)
它向我显示了一个空字符串。为什么会这样?
如果下面的xpath
//li//a[@href='/cristiano/followers']
在HTML-DOM
中是唯一的,那么你可以使用下面的代码:
wait = WebDriverWait(driver, 20)
try:
a_tag = wait.until(EC.visibility_of_element_located((By.XPATH, "//li//a[@href='/cristiano/followers']")))
print(a_tag.find_element(By.XPATH, ".//descendant::span").get_attribute('title'))
except:
print("something went wrong")
pass
这个用例实际上不需要循环。
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
检查元素是否唯一的步骤:
Press F12 in Chrome
-> 转到 element
部分 -> 执行 CTRL + F
-> 然后粘贴 xpath
并查看是否需要 element
正在 突出显示 与 1/1
匹配节点。