16

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]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shartshooter
  • 1,761
  • 5
  • 19
  • 40

2 Answers2

22

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'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
har07
  • 88,338
  • 12
  • 84
  • 137
  • the issue I'm facing is that `innerTEXT` is returning null, but `innerHTML` is not – shartshooter Mar 14 '17 at 05:11
  • @kmomo It isn't literally `innerTEXT` that I meant, see the code snippet above. Looks like `innerHTML` is Selenium thing, so you might be able to use it. Only your XPath expression is a bit off, `=` should be replaced with `,` : `contains(@innerHTML,'someUniqueString')` – har07 Mar 14 '17 at 06:05
  • that's not working - `driver.find_element_by_xpath("//*[contains(@innerHTML,'someUniqueString')]")` is returning `Message: Unable to locate element: //*[contains(@innerHTML,'someUniqueString')]` – shartshooter Mar 14 '17 at 19:02
  • 2
    @kmomo did you try the XPath in this answer too? `//*[text()[contains(., 'someUniqueString')]]` – har07 Mar 15 '17 at 01:03
4

Try the following XPath expression:

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

Note: As the code element is set with display: none, the element is not visible, though you can find the element. If you try to interact with the element using click or other API, you might get ElementNotVisisbleException. To put it simply, you can't interact with the elements which are not displayed in the GUI (browser).


If you are not able to find the element, then there are good chances that your element is inside an iframe. In such cases, you must switch to the frame first and then use the XPath to find the element.

More details on switching between frames are here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65