Omar Yacop
Omar Yacop

Reputation: 305

How to get text from an element with selenium

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}"]]]]')

Upvotes: 0

Views: 52

Answers (1)

Tal Angel
Tal Angel

Reputation: 1788

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.

Upvotes: 1

Related Questions