How do I detect text inside of this, this is HTML & this is XPath. I tried

    <span class="messageText">Hello.</span>

I tried with this but it didn't work, it said no such element found.

driver.find_element(By.XPATH, '//*[@id="liveAgentChatLogText"]//*[contains(string(.), "Hello")]')
  • 1
    Please don't post multiple near-identical questions - instead edit your first question to add the information you need. Thanks! – Kate Paulk Sep 6 '18 at 11:51

You can refer any of the xpath which is given below, to locate the element using text :


Here you have to pass the complete sting which your element has else it wont work.



You can use contains method in xpath to get your element matching based on full or partial given text.


driver.find_element_by_xpath("//span[@class='messageText'][contains(.,'Hello.')]")   //recommended 

To make more robust you can include attribute of that particular tag. here i have used class attribute of span, that means span which have class=messageText and contain text=Hello.

Still you are facing issue in getting element, try ExplicitWaits until they are loaded in DOM.

| improve this answer | |

As per the given HTML you can use below mentioned xpath that we usually use in automation testing company.



You can use contains method in xpath as well to get the element text partially. Also to make it more specific (or robust) can include attribute of that particular tag



Hope above solution will help and if you still face issue in getting the element try to wait for visibility of element or please provide more line of code (HTML)


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.