0

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

HTML
    <span class="messageText">Hello.</span>
XPath
    //*[@id="liveAgentChatLogText"]/span[22]/span[2]

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
0

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

driver.find_element_by_xpath("//span[text()='Hello.']")

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

OR

driver.find_element_by_xpath("//span[contains(.,'Hello.')]")

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

OR

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.

0

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

driver.find_element_by_xpath("//span[text()='Hello.']")

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

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

driver.find_element_by_xpath("//span[contains(text(),'Hello.')]")

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

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)

Thanks

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.