给定一个(python)selenium WebElement可以得到innerText吗?

下列

element = driver.execute_script("return $('.theelementclass')")[0]

找到我的元素(只包含一些文本的div),但是:

element.text

返回一个空字符串(这是一个惊喜).从Java脚本控制台:

$('.theelementclass').text  # also (consistently) empty
$('.theelementclass').innerText  # YES! gets the div's text.

所以,我的问题是,鉴于我有一些WebElement,我可以找到innerText吗? (由于无聊的原因,我想使用找到的webelement而不是原始查询进行操作).

我包括周围的HTML.但是,我不认为它有用(因为元素被找到并且是唯一的)

<first-panel on-click="onClickLearnMore()" class="ng-isolate-scope">
  <div class="comp-onboarding-first-thought-panel">
    <div class="onboarding-text-container">
        <div class="theelementclass">
                Congratulations.
                You found the text is here!
        </div>
        <div class="button-cont">
            <div ng-click="onClickButton()">Learn more about The Thing</div>
        </div>
    </div>
</div>
</first-panel>

解决方法:

您可以将webelement传递给js代码

element = driver.find_element_by_css_selector('.theelementclass')
inner_text= driver.execute_script("return arguments[0].innerText;", element)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐