Python Forum
Selenium innerHTML list, print specific value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Selenium innerHTML list, print specific value (/thread-33948.html)



Selenium innerHTML list, print specific value - denis22934 - Jun-12-2021

Hello,

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! Heart


RE: Selenium innerHTML list, print specific value - snippsat - Jun-13-2021

(Jun-12-2021, 03:35 PM)denis22934 Wrote: How do I make python find "value" from a variable in InnerHTML ? Please note that there is a colon after "value"!
If i do test local with that html,after have find pre tag with CSS selector,it's text then can use regex to get value.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import re

#--| Setup
options = Options()
options.add_argument("--headless")
browser = webdriver.Chrome(executable_path=r'C:\cmder\bin\chromedriver.exe', options=options)
#--| Parse or automation
browser.get('file:///E:/div_code/scrape/local3.html')
tag_pre = browser.find_elements_by_css_selector('body > pre')
tag = tag_pre[0].text.strip()
print(tag)
print('-' * 20)
value = re.search(r"\"value\"\s:\s(\d+)", tag)
print(value.group(1))
Output:
"amount": 12{ "value" : 3 }, -------------------- 3



RE: Selenium innerHTML list, print specific value - denis22934 - Jun-14-2021

(Jun-13-2021, 03:30 PM)snippsat Wrote:
(Jun-12-2021, 03:35 PM)denis22934 Wrote: How do I make python find "value" from a variable in InnerHTML ? Please note that there is a colon after "value"!
If i do test local with that html,after have find pre tag with CSS selector,it's text then can use regex to get value.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import re

#--| Setup
options = Options()
options.add_argument("--headless")
browser = webdriver.Chrome(executable_path=r'C:\cmder\bin\chromedriver.exe', options=options)
#--| Parse or automation
browser.get('file:///E:/div_code/scrape/local3.html')
tag_pre = browser.find_elements_by_css_selector('body > pre')
tag = tag_pre[0].text.strip()
print(tag)
print('-' * 20)
value = re.search(r"\"value\"\s:\s(\d+)", tag)
print(value.group(1))
Output:
"amount": 12{ "value" : 3 }, -------------------- 3

Thank you for your response. But I already found the solution for my question.