Andreas
Andreas

Reputation: 9

How to retrieve text by xpath

Hey everyone

I am 15 years old and I'm currently making a python/selenium script that automatically loges in on my school page and goes in to see what homework i have tomorrow.

The way I want it to check if we have received homework is if the text box which is: "LB MAT 86" is something else (because that means we have received homework)

Let me now tell you about my problem:

Picture: https://i.sstatic.net/WLjuR.png

But first, let me tell u how my "homework page" is designed and works.

On the picture you see that I have inspected the text: "LB MAT 86".

When we get homework our teacher deletes the text "LB MAT 86" and instead writes the homework we get.

My question for you is how can I find the text. The only way I can see how you can find the text is by its location.

The xpath of the text field: //*[@id="sk-diary-notes-container"]/div/div[1]/div/table/tbody/tr[3]/td[3]/text()[1]

The full xpath of the text field: /html/body/div[1]/div[2]/div[3]/div[2]/div/div/div[1]/div/table/tbody/tr[3]/td[3]/text()[1]

OuterHTML:   LB MAT 86

Thanks

Upvotes: 0

Views: 85

Answers (2)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

driver.find_element_by_xpath("//table[contains(@align,'center')//td[contains(.,'LB MAT 86')]")

Would get the element with text LB MAT 86.

You can add some code to find the current index to check for your code. /tr[i]/td[i]

Upvotes: 1

vitaliis
vitaliis

Reputation: 4212

Try using this xpath

locator = driver.find_element_by_xpath("//table[contains(@align,'center')/tbody/tr[3]/td[3]")

You should get everything including nbsp (non-braking space). Then split the result and get everything after nbsp;

You did not include html code, only screenshot. Next time include it (or include it now). After you test the correct locator, try to use split(). It will get the last element from your text divided by space. I am not 100% sure it will work, experiment by yourself.

mystring = locator.text
desired_text = result = mystring.split(' ')[-1]

Upvotes: 0

Related Questions