python - 如何在 Selenium 中获取 'nth-of-type'

标签 python selenium automated-tests

我正在使用 Selenium Webdriver 检查此特定段落的文本(此处以蓝色突出显示的段落):

enter image description here

但是我如何“查询”该段落?

这就是我正在尝试的(不起作用):

    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”对象没有属性“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

关于python - 如何在 Selenium 中获取 'nth-of-type',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51449252/

相关文章:

python 使用逗号交换值会引起混淆

html - Selenium:测试元素是否包含一些文本

Python selenium lib 未在 Windows 上安装

javascript - 如何在 Testcafe 中使用 Javascript 将表作为数组返回

python - 如何考虑同一 DAG 中先前任务的结果来创建动态数量的任务?

python - 如何使用 Scikit-Image 库从 Python 中的 RGB 图像中提取绿色 channel ?

javascript - cucumber /自动化测试 : How to approach the overall concept using javascript functions?

android - 简单地从espresso android中的自定义 View 获取 View

python - 为什么以不同的方式生成数组会导致代码不同部分的大幅加速?

python - 使用 Selenium (Python) 获取输入框的值