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

use*_*956 10 python selenium webdriver

下列

element = driver.execute_script("return $('.theelementclass')")[0]
Run Code Online (Sandbox Code Playgroud)

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

element.text
Run Code Online (Sandbox Code Playgroud)

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

$('.theelementclass').text  # also (consistently) empty
$('.theelementclass').innerText  # YES! gets the div's text.
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,鉴于我有一些 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>
Run Code Online (Sandbox Code Playgroud)

Ali*_*jad 12

这是一个更简单的方法:

element = driver.find_element_by_class_name('theelementclass')
text = element.get_attribute('innerText')
Run Code Online (Sandbox Code Playgroud)

因此,您可以使用get_attribute()方法对“ outerHTML ”、“ href ”、“ src ”等进行类似的操作。