python - 如何在 selenium python 中获取 h2 标签内的文本
问题描述
如何获取 html 标记内的文本并断言它是否存在于页面上。
html-
<h2> You are suggested the Low Fat Vegan Diet </h2>
我试过了-
message = driver.find_elements_by_tag_name('h2').Text()
AttributeError:“列表”对象没有属性“文本”
message = driver.find_elements_by_tag_name('h2').getText()
AttributeError:“列表”对象没有属性“getText”
我如何获取文本并断言它存在于页面中?
解决方案
方法名称是 find_elements_by_tag_name
这样它返回一个列表elements
。如果您的目标元素是元素中的第一项,您可以试试这个:
message = driver.find_elements_by_tag_name('h2')[0].text
我建议您使用Beautifulsoup来更好地搜索元素。
推荐阅读
- r - 如何分配门票
- node.js - 在第一次自动使用之后维护 google 客户端 API refresh_token
- java - 如何满足具有布尔条件的谓词
- http - Go http handler 路由未注册,路由不一致
- linux - 如何在linux中水平打印awk输出
- java - org.json.JSONException:java.lang.String 类型的 HTTP 值无法转换为 JSONObject
- c++ - 是否应该为类中的每个 setter 设置互斥锁,或者使所有成员变量原子化以实现线程安全?
- json - 如何解码这个json字符串
- java - 当实际的jvm安装发生时?
- mysql - 如何将mysql数据返回给调用者?某些工作,其他情况不工作?