以下是
element = driver.execute_script("return $('.theelementclass')")[0]
查找我的元素(只包含一些文本的div ),但是:
element.text
返回一个空字符串(这是一个惊喜)。从JavaScript控制台:
$('.theelementclass').text // Also (consistently) empty
$('.theelementclass').innerText // YES! Gets the div's text.
考虑到我有一些WebElement,我能找到innerText吗?(出于无聊的原因,我想使用已发现的异常,而不是原始的查询。)
我包括了周围的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>
发布于 2020-05-15 17:11:03
这里有一个更简单的方法:
element = driver.find_element_by_class_name('theelementclass')
text = element.get_attribute('innerText')
因此,您可以使用get_attribute()方法对'outerHTML‘、'href’、'src‘等进行类似的操作。
发布于 2015-05-15 08:35:36
您可以向JavaScript代码传递一个提示:
element = driver.find_element_by_css_selector('.theelementclass')
inner_text = driver.execute_script("return arguments[0].innerText;", element)
发布于 2015-05-15 10:48:50
innerText
是特定于Internet的。如果需要跨平台字段,请使用textContent
。
driver.execute_script("return arguments[0].textContent", element)
element
是一个已经获得的WebElement
。
顺便说一下,你说你在控制台上试过这个:
$('.theelementclass').text
它不能工作,因为.text
是一个函数。必须叫它。
https://stackoverflow.com/questions/30204029
复制相似问题