Search code examples
pythonseleniuminnerhtml

Selenium innerHTML list, print specific value


First of all, I'm new to working with Python, especially Selenium. So I connected to a page with the webdriver and also already grabbed the InnerHTML I need. Here's my problem, InnerHTML is a "list" and I only want to output one value. It looks something like this:

<html>
 <body>
  <pre style="example" xpath="1">
   "amount": 12{
   "value" : 3
    },
  </pre>
 </body>
</html>

^It's just for illustration, because the actual thing is much longer. InnerHTML looks like this:

"amount": 12{
   "value" : 3
    },

^This is where I am now. I can't specify a line because the page is not static. How do I make python find "value" from a variable in InnerHTML ? Please note that there is a colon after "value"!

Thank you very much in advance!


Solution

  • I suggest using regular expression to find the value. I assume that you only need the number part, so here's the code:

    innerHTML = '''
    "amount": 12{
       "value" : 3
        },"value":4
        'value': 5
    '''
    
    import re
    regex = re.compile(r'''("|')value("|')\s*:\s*(?P<number>\d+)''')
    startpos = 0
    m = None
    
    while 1:
        m = regex.search(innerHTML, startpos)
        if m is None: break
        print(m.group("number"))
        startpos = m.start() + 1
    
    # output:
    # 3
    # 4
    # 5
    

    This will print out all the value numbers found, as strings. You can convert them to integers afterwards, for example.
    NOTE: My code also accounts for the case value is surrounded by single quotes ' rather than double quotes ". This is for your convenience; if not, you can change the appropriate line above to:

    regex = re.compile(r'''"value"\s*:\s*(?P<number>\d+)''')
    

    In that case, the output would not include the value 5.