这是我的代码:print(browser.find_element_by_partial_link_text("followers").get_attributes("title"))。我正在使用 find_element_by_partial_link_text 因为 xpath 可能对每个用户都是唯一的。但是我的代码没有返回任何内容。这是元素的地址:
<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 获取标题中的值?提前致谢。
已解决:您可以查看 DebanjanB 的答案。
请您参考如下方法:
标题 的文本,即 3,862,378 不在任何 <a> 中标签。因此你不能使用 find_element_by_partial_link_text() .此外,该元素是一个动态元素,因此您必须为所需的 visibility_of_element_located() 引入 WebDriverWait您可以使用以下解决方案:
使用
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()
