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

2023-12-14

我正在使用 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' 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Selenium 中获得“nth-of-type” 的相关文章

随机推荐