How to get text from an element with selenium

Question:

I am trying to get text from an element and then searching for it in another place, the problem is that when I get the text using .text, I noticed that it misses if there is two spaces, and when it searches in the next page, it can’t find it. So is there a way to get text as it is with spaces?

Code:

self.name = session.find_element('xpath', './/a[contains(@href, "name")]').text
self.dataBrowser.find_elements(By.XPATH, f'//tr[child::td[child::div[child::strong[text()="{self.name}"]]]]')
Asked By: Omar Yacop

||

Answers:

Not always the text property will actually hold the text. So, try this:

element = browser.find_element(By.CSS, 'CSS_EXPRESSION')
text = element.get_attribute('text')
if text is None or text is '':
    text = element.get_attribute('value')

If text and value both returns nothing just try reading the innnerHTML attribute.

Answered By: Tal Angel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.