-1

im currently writing a script which connects to a website, asks you for a transaction address, forwards you to that transaction and now im trying to scrape the data which was gathered during that transaction, e.g the amount which was recieved or the amount that left etc. Im struggling to figure out how to print the data, i think i have the correct code to find it on the page using XPATH. If not and help is appreciated :)

The website im trying to access is https://creeper.banano.cc/explorer/block/8F0604B65C973F931D6E909ACD819D276AB1DF4C1FF161E754D509075C4BD2EC

The text im trying to get out is the Original Block Content at the bottom, i assume once i have that i can tell python exactly which parts of it i need printed out.

When it asks you for the user transaction use this: 8F0604B65C973F931D6E909ACD819D276AB1DF4C1FF161E754D509075C4BD2EC

from selenium import webdriver 

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys 
import time 

USER = input('Enter the user transaction:')
PATH = "C:\Program Files (x86)\chromedriver.exe" 
driver = webdriver.Chrome(PATH) 

driver.get("https://creeper.banano.cc/")

search = driver.find_element(By.CSS_SELECTOR,".ValidatedSearch.form-control.form-control-lg") 
search.send_keys(USER) 

link = driver.find_element(By.CSS_SELECTOR,".btn.btn-nano-primary.btn-lg")  
link.click()

time.sleep(2)

Data = driver.find_element(By.XPATH,"//*[@id='Content']/div/div[2]/div/div/pre/code")
print(Data)

Instead my output is as follows:

<selenium.webdriver.remote.webelement.WebElement (session="ca8ada9757ad332e62fcc8591f840d1b", element="8dd6e81e-114d-4073-95b1-6907fe20eece")>

Any help is appreciated :)

1 Answers1

1

You are printing the WebElement. Hence you see the output as:

<selenium.webdriver.remote.webelement.WebElement (session="ca8ada9757ad332e62fcc8591f840d1b", element="8dd6e81e-114d-4073-95b1-6907fe20eece")>

To print the text within the <code> </code> tag you can use either of the following locator strategies:

  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element(By.CSS_SELECTOR, "div#Content pre.text-monospace.bg-light.rounded.p-3 > code").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//div[@id='Content']//pre[@class='text-monospace bg-light rounded p-3']/code").text)
    

To extract the desired text ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR and text attribute:

    driver.get('https://creeper.banano.cc/explorer/block/8F0604B65C973F931D6E909ACD819D276AB1DF4C1FF161E754D509075C4BD2EC')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#Content pre.text-monospace.bg-light.rounded.p-3 > code"))).text)
    
  • Using XPATH and get_attribute("innerHTML"):

    driver.get('https://creeper.banano.cc/explorer/block/8F0604B65C973F931D6E909ACD819D276AB1DF4C1FF161E754D509075C4BD2EC')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='Content']//pre[@class='text-monospace bg-light rounded p-3']/code"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Console Output:

    {
      "block_account": "ban_1bananobh5rat99qfgt1ptpieie5swmoth87thi74qgbfrij7dcgjiij94xr",
      "amount": "1453108524228114099665371136",
      "balance": "1649771394008524228122087423772876",
      "height": "60",
      "local_timestamp": "1644867974",
      "confirmed": "true",
      "contents": {
        "type": "state",
        "account": "ban_1bananobh5rat99qfgt1ptpieie5swmoth87thi74qgbfrij7dcgjiij94xr",
        "previous": "D48C6FE84BA29276F4D7339586F8B137405CCCB4E897F53DFDD5CC420824F811",
        "representative": "ban_1bananobh5rat99qfgt1ptpieie5swmoth87thi74qgbfrij7dcgjiij94xr",
        "balance": "1649771394008524228122087423772876",
        "link": "07EDFD3721B5F0ED66860D5BADC7FACD29D54A31257ADE3D16C1E235C957F647",
        "link_as_account": "ban_13zfznuk5fhixomae5cuoq5zombbto754bdturyjfih48q6ohxk9e8azwybh",
        "signature": "9FF4B7432DAE7D0CF9923785C16EB7928F0D1A625C8554755A0F43E695885E3C2D030D33B0A45C43FE2F26A339C0546AE0F179A11BA5A7A4EDED1A179AA4990F",
        "work": "be085b9c3c201ecf",
        "subtype": "receive"
      },
      "subtype": "receive",
      "pending": "0",
      "source_account": "ban_3p1anetee7arfx9zbmspwf9c8c5r88wy6zkgwcbt7rndtcqsoj6fzuy11na3",
      "timestamp": "1644867974832"
    }
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Still getting used to this, you're help is much appreciated and explains the process a lot and the working behind it, not sure if im meant to give feedback here but thanks again! P.S. i added .text at the end of my original code and it printed the exact same output that yours resulted in. – Taksiepacze Mar 17 '22 at 23:44