其他分享
首页 > 其他分享> > 如何在Selenium中获得’nth-of-type’

如何在Selenium中获得’nth-of-type’

作者:互联网

我正在使用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,automated-tests
来源: https://codeday.me/bug/20191008/1875426.html