問題描述
這是我的代碼:print(browser.find_element_by_partial_link_text(followers").get_attributes(title"))
.我正在使用 find_element_by_partial_link_text
因為每個用戶的 xpath 可能是唯一的.但是我的代碼什么也沒返回.這是元素的地址:
This is my code : print(browser.find_element_by_partial_link_text("followers").get_attributes("title"))
. I am using find_element_by_partial_link_text
because maybe the xpath could be unique for each user. But my code returns nothing. This is the element's adress:
<li class=" LH36I">
<a class=" _81NM2" href="/username/followers/">
<span class="g47SY lOXF2" title="3,862,378">3.8m</span>
" followers"
</a>
</li>
所以我的問題是如何使用 find_element_by_partial_link_text
獲取標(biāo)題中的值?提前致謝.
So my question is how can i get the value in the title using find_element_by_partial_link_text
? Thanks in advance.
已解決:您可以查看 DebanjanB 的答案.
SOLVED : You can look at DebanjanB 's answer.
推薦答案
title 即 3,862,378 的文本不在任何 <a>
標(biāo)簽.因此你不能使用 find_element_by_partial_link_text()
.此外,該元素是一個動態(tài)元素,因此您必須為所需的 visibility_of_element_located()
誘導(dǎo) WebDriverWait,您可以使用以下解決方案:
The text of the title i.e. 3,862,378 isn't within any <a>
tag. Hence you can't use find_element_by_partial_link_text()
. Moreover the element is a dynamic element so so you have to induce WebDriverWait for the desired visibility_of_element_located()
and you can use the following solution:
使用
XPATH
:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "http://a[@href='/username/followers/' and contains(., 'followers')]/span"))).get_attribute("title"))
使用 CSS_SELECTOR
:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[href='/username/followers/']>span"))).get_attribute("title"))
注意:根據(jù)文檔,實際方法是 get_attribute(name)
但不是 get_attributes()
Note: As per the documentation the actual method is get_attribute(name)
but not get_attributes()
這篇關(guān)于如何使用 Python 通過 Selenium 檢索標(biāo)題屬性?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!