这是我的代码: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 检索标题属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)