The following
element = driver.execute_script("return $('.theelementclass')")[0]
find me my element (a div that ONLY contains some text), but:
element.text
returns an empty string (which is a surprise). From the Java script console:
$('.theelementclass').text # also (consistently) empty
$('.theelementclass').innerText # YES! gets the div's text.
So, my question is, given that I have some WebElement, can I find the innerText? (For boring reasons, I want to operate with a found webelement, not the original query).
I'm including the surrounding HTML. However, I don't think its useful (since element is found and is unique)
<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>
getText() Method in Selenium This method helps retrieve the text, which is basically the innertext of a WebElement. getText() method returns string as a result. It removes the whitespaces if present in the front and back of the string.
We can get the text from a website using Selenium webdriver USING the getText method. It helps to obtain the text for a particular element which is visible or the inner text (which is not concealed from the page).
Here is a more simple approach:
element = driver.find_element_by_class_name('theelementclass')
text = element.get_attribute('innerText')
So you can do similar stuff with 'outerHTML', 'href', 'src' etc. with get_attribute() method.
You can pass webelement to js code
element = driver.find_element_by_css_selector('.theelementclass')
inner_text= driver.execute_script("return arguments[0].innerText;", element)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With