Skip to content Skip to sidebar Skip to footer

Getting Value After Button Click With BeautifulSoup Python

I'm trying to get a value that is given by the website after a click on a button. Here is the website: https://www.4devs.com.br/gerador_de_cpf You can see that there is a button ca

Solution 1:

open_browser and get_cpf are absolutely not related to each other...

Actually you don't need get_cpf at all. Just wait for text after clicking the button:

from selenium.webdriver.support.ui import WebDriverWait as wait

def open_browser():
    driver = webdriver.Chrome("/home/felipe/Downloads/chromedriver")
    driver.get(url)
    driver.find_element_by_id('bt_gerar_cpf').click()
    text_field = driver.find_element_by_id('texto_cpf')
    text = wait(driver, 10).until(lambda driver: not text_field.text == 'Gerando...' and text_field.text)
    return text

print(open_browser())

Update

The same with requests:

import requests

url = 'https://www.4devs.com.br/ferramentas_online.php'
data = {'acao': 'gerar_cpf', 'pontuacao': 'S'}
response = requests.post(url, data=data)
print(response.text)

Solution 2:

You don't need to use requests and BeautifulSoup.

from selenium import webdriver
from time import sleep

url = "https://www.4devs.com.br/gerador_de_cpf"

def get_cpf():
    driver = webdriver.Chrome("/home/felipe/Downloads/chromedriver")
    driver.get(url)
    driver.find_element_by_id('bt_gerar_cpf').click()
    sleep(10)
    text=driver.find_element_by_id('texto_cpf').text
    print(text)
get_cpf()

Solution 3:

Can you use a While loop until text changes?

from selenium import webdriver

url = "https://www.4devs.com.br/gerador_de_cpf"

def get_value():
    driver = webdriver.Chrome()
    driver.get(url)
    driver.find_element_by_id('bt_gerar_cpf').click()
    while driver.find_element_by_id('texto_cpf').text == 'Gerando...':
        continue
    val = driver.find_element_by_id('texto_cpf').text
    driver.quit()
    return val

print(get_value())

Solution 4:

I recommend this website that does exactly the same thing.

https://4devs.net.br/gerador-cpf

But to get the "gerar cpf" action with selenium, you can inspect the HTML source code with a browser and click on "copy XPath for this element".

It is much simpler than manually searching for the elements in the page.


Post a Comment for "Getting Value After Button Click With BeautifulSoup Python"