Python Forum
Selenium innerHTML list, print specific value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selenium innerHTML list, print specific value
#1
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
Reply
#2
(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
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get specific TD text via Selenium? euras 3 8,655 May-14-2021, 05:12 PM
Last Post: snippsat
  With Selenium create a google Search list in Incognito mode withe specific location, tsurubaso 3 3,186 Jun-15-2020, 12:34 PM
Last Post: tsurubaso
  Error in Selenium: CRITICAL:root:Selenium module is not installed...Exiting program. AcszE 1 3,587 Nov-03-2017, 08:41 PM
Last Post: metulburr
  Unable to print data while looping through list in csv for webscraping - Python Prince_Bhatia 1 3,476 Oct-04-2017, 11:18 AM
Last Post: wavic
  Python+Selenium+PhantomJS with proxy list technoir 0 8,647 Jan-17-2017, 05:22 AM
Last Post: technoir

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020