如何在Selenium中获取'第n个类型'

2024-10-03 00:19:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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'


Tags: text文本selfbycontainerseleniumelementfind
2条回答

只需使用此css选择器来执行以下操作:

#visual-7 .hed-dek-wrapper >p:nth-of-type(2)  

然后使用css选择器找到元素,它将给您第二个p标记。在

^{pr2}$

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

相关问题 更多 >