python - 如何在 Selenium 中获得“nth-of-type”
问题描述
我正在使用 Selenium Webdriver 检查这个特定段落的文本(此处以蓝色突出显示的那个):
但是我如何“查询”该段落?
这就是我正在尝试的(不工作):
def test_intro_text(self):
"""Test that intro text is expected text"""
container = self.browser.find_element_by_id('visual-17')
hed_dek_wrapper = container.find_element_by_class_name('hed-dek-wrapper')
intro_text = hed_dek_wrapper.findElement('p:nth-of-type(2)')
self.assertIn(
'Over the past 20 years, we have seen an evolution.',
intro_text.text
)
我收到此错误:AttributeError:'WebElement' object has no attribute 'findElement'
解决方案
findElement()
不是 Python。请尝试以下方法:
intro_text = hed_dek_wrapper.find_element_by_css_selector('p:nth-of-type(2)')
assert 'Over the past 20 years, we have seen an evolution.' in intro_text.text
推荐阅读
- azure - 如何及时获取 Azure 应用服务的实例数量
- c# - C# - VS Code - 启动:程序...不存在
- python - 如何从给定范围生成固定随机数,在每次执行中产生相同的输出
- vba - Excel查找空白单元格
- java - Android studio 蓝牙连接 App 崩溃
- javascript - 三元运算符在反应js渲染功能中不起作用
- machine-learning - 预测分析 - “为什么”因素和模型可解释性
- java - 如何按索引更新整数数组索引的元素
- javascript - 使用固定列构建可滚动表
- docker - 使用 Docker Engine API 设置容器的 CPU 核心数