Home > Back-end >  Can't pull text from xpath selenium
Can't pull text from xpath selenium

Time:08-29

so i have this xpath(its inside another document xpvar):

tiltakstekst = "//tbody[@data-bind='foreach: WorkOrders']/tr/td/div/div/div/div/div[@class='comments-field-readonly']"

Im attempting to pull the text using this code("x" is because its in a for-loop).

tiltakstekst = driver.find_elements(By.XPATH, xpvar.tiltakstekst)[x].text

the HTML at the end is like this:

<div >
    <div  data-bind="html: Description" readonly="">360 Realfagbygget – ombygning av rom BU2-117 og BU2-123 til samme rom. - Levering av brannblits, merkes med merkeskilt «brannalarm». 
    </div>
</div>

I find and highlight the wanted div class when using the inspector with the xpath i have here inside Chrome, but i cant seem to pull any text from it. I get no text, so i dont even get any errors. When doing the same using CSS_SELECTOR div.comments-field-readonly i get "None" as text.

I have done exactly the same and got it working on other parts of the website, so i dont understand why i dont get the text.. Any suggestions?

CodePudding user response:

Found the answer, the element wasn't visible so I couldn't use .text

Using get_attribute('textContent') solved my issue.

CodePudding user response:

Not always text attribute will have something inside it, so you will have to try this:

element.getAttribute('text')

Or

element.getAttribute('value') 

And...if that doesn't work you can always find innerHTML.

  • Related