Home > other >  How to get text from an element with selenium
How to get text from an element with selenium

Time:09-18

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

CodePudding user response:

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.

  • Related