小能豆

如何在 Selenium 中获取“nth-of-type”

py

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

1.png

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

这是我正在尝试的(没有效果):

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’


阅读 48

收藏
2024-11-27

共1个答案

小能豆

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
2024-11-27