htmlpython-2.7seleniumxpathbeautifulsoup

Selenium - XPath - Searching for an element by the innerHTML


I'm learning Selenium and have a decent grasp of XPath.

An issue I'm running into is that on a web page, there's an element I want to select that has a dynamically generated id and class. I had tried the following:

code = driver.find_element_by_xpath("//*[contains(@text='someUniqueString')]")

However, the element doesn't have any text. Instead it's a <code> element with JSON.

<codestyle="display: none" id="something-crazy-dynamic">
    {"dataIWantToGrab":{"someUniqueString":...}}
</code>

I'm looking to search the innerHTML to find a unique string using CPU, but I can't find any good resources.

I've tried

driver.find_element_by_xpath("//*[contains(@innerHTML='someUniqueString')]")

but I am receiving the error

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[contains(@innerHTML='someUniqueString')]

Below is a link to the sibling text I'm working with

https://gist.github.com/anonymous/b227e59c942e7ec9f5a851a3b7ecdfc6


I was able to get around this, not by using Selenium, but with Beautiful Soup. It is not ideal, but it is still a solution.

soup = BeautifulSoup(driver.page_source)
codes = soup.find_all("code")
found_json = [i for i in codes if i.text.find("someUniqueString") > 0]

Solution

  • You can't use XPath to match by inner HTML, but you can use it to match by 'inner text':

    //*[text()[contains(., 'someUniqueString')]]
    

    `demo

    The above XPath expression should return the code element since it is the parent element of the target text 'someUniqueString'.