I’m trying to extract the text value following a b tag that contains specific text. I’m using Selenium web driver with Python3.
The HTML inspected for the value I’m trying to return (11,847) is here:
This has an Xpath below (I’m not using this xpath directly to find the element as the table construction changes for different examples that I plan to iterate through):
/html/body/form[1]/div[2]/table[2]/tbody/tr[3]/td[2]/text()
As an example, when I print the below it returns Att: i.e. the element located by my search for the text ‘Att’ within the b tags.
att=driver.find_element("xpath",".//b[contains(text(), 'Att')]").text
print(att)
Is there a way I can return the value following <b>Att:</b>
by searching for ‘Att:’ (or conversly, I’d also like to return the value following <b>Ref:</b>
.
Thanks in advance.
Advertisement
Answer
11,847
text content belongs to td
node.
You can locate this td
element by it’s child b
text content.
Then you will be able to retrieve the entire text content of that td
node.
It will contain Att:
and extra spaces and the desired 11,847
string.
Now you will need to remove the Att:
and extra spaces so only 11,847
will remain.
As following:
#get the entire text content entire_text = driver.find_element(By.XPATH,"//td[.//b[contains(text(), 'Att')]]").text #get the child node text content child_text = driver.find_element(By.XPATH,"//b[contains(text(), 'Att')]").text #remove child text content from entire text content goal_text = entire_text.replace(child_text,'') #trim white spaces goal_text = goal_text.strip()