Jay Wright Jay Wright - 1 year ago 156
HTML Question

Using Python & Selenium, how to extract the text from HTML containing the <p> tag?

This I know is a very simple question. I'm quite sick and trying to finish up this presentation and my brain just doesn't seem to be working right.

The HTML code is as follows:

<b>Postal code:</b>

The defect is the zipcode text field is only accepting four characters. Once submitted, I'm trying to grab the number "3502" in this case and use len to count them.


The problem is that you cannot directly locate the "text" nodes with find_element_* commands in selenium - the locators you use have to point to actual elements.

In this case, I would get the p element's text, split by : and get the last item:

text = driver.find_element_by_xpath("//p[b = 'Postal code:']").text
postal_code = text.split(":")[-1].strip()