问题描述
这是我的代码: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
获取标题中的值?提前致谢.
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>
标签.因此你不能使用 find_element_by_partial_link_text()
.此外,该元素是一个动态元素,因此您必须为所需的 visibility_of_element_located()
诱导 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, "//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"))
注意:根据文档,实际方法是 get_attribute(name)
但不是 get_attributes()
Note: As per the documentation the actual method is get_attribute(name)
but not get_attributes()
这篇关于如何使用 Python 通过 Selenium 检索标题属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!