简体   繁体   中英

How to use selenium to scrape data that is generated on the website?

I am implementing a Python code wherein I need to generate an SHA key. To do this I use an online SHA generator. I send the input (the data for which the Hash is needed), through selenium, which works successfully. However, after that I am unable to get the output that is generated(the text string). I use the find_element_by_xpath function to get this data but it only returns an empty string. I don't understand what i am doing wrong. Can somebody tell me how I can do this? Or if there is any other method, besides using Selenium to achieve this?

I have used the following code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC

def driver_init():

    driver = webdriver.Chrome(executable_path='chromedriver.exe')
    driver.wait=WebDriverWait(driver,5)
    return driver

def get_data(driver, val):

    driver.get('https://passwordsgenerator.net/sha1-hash-generator/')
    xpath='//*[@id="txt1"]'
    box=driver.find_element_by_xpath(xpath)
    box.send_keys(val)
    element=driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="txt2"]')))
    return element.text

driver=driver_init()
w=get_data(driver, '100011')

`

The reason you're not seeing that text populate to your variable is because it's value is being set by scripts running on the page. You can get the value by using element.get_attribute('value') to get the element's current value.

A word of caution though: these types of hashes are generally used for security purposes, and unless you strongly trust this website, I would suggest using a local solution like an SSL (openSSL, cryptography, etc.) library to resolve these hashes instead of sending requests over the web. If something were to happen to this site's server or if it was taken over by a malicious actor, they could modify the site to send them all your (previously) secure data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM