I'm trying to get a value that is given by the website after a click on a button.
Here is the website: CPF generator website
You can see that there is a button called "Gerar CPF", this button provides a number that appears after the click.
My current script opens the browser and get the value, but I'm getting the value from the page before the click, so the value is empty. I would like to know if it is possible to get the value after the click on the button.
from selenium import webdriver
from bs4 import BeautifulSoup
from requests import get
url = "https://www.4devs.net.br/gerador-cpf"
def open_browser():
driver = webdriver.Chrome("/home/willi/Documents/chromedriver")
driver.get(url)
driver.find_element_by_class('btn m-1 btn-primary btn-sm').click()
def get_cpf():
response = get(url)
page_with_cpf = BeautifulSoup(response.text, 'html.parser')
cpf = page_with_cpf.find("input", {"id": "__BVID__61"}).text
print("The value is: " cpf)
open_browser()
get_cpf()
CodePudding user response:
You could simply add a while
loop in order to update the value at a time you desire. For example:
import time
from selenium import webdriver
from bs4 import BeautifulSoup
from requests import get
url = "https://www.4devs.net.br/gerador-cpf"
def open_browser():
driver = webdriver.Chrome("/home/willi/Documents/chromedriver")
driver.get(url)
driver.find_element_by_class('btn m-1 btn-primary btn-sm').click()
def get_cpf():
response = get(url)
page_with_cpf = BeautifulSoup(response.text, 'html.parser')
cpf = page_with_cpf.find("input", {"id": "__BVID__61"}).text
print("The value is: " cpf)
while True:
open_browser()
get_cpf()
time.sleep(your_time_in_s)
CodePudding user response:
The following code works using part of Ruggero's solution and pyperclip:
import time
import pyperclip
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from requests import get
import chromedriver_binary # Adds chromedriver binary to path
url = "https://www.4devs.net.br/gerador-cpf"
your_time_in_s=2
def open_browser():
driver = webdriver.Chrome()
driver.get(url)
driver.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/div/div/div[3]/div/div[3]/div[1]/div/button[2]').click()
time.sleep(your_time_in_s)
driver.find_element(By.XPATH, '//*[@id="__BVID__60"]/div/div/div/button').click()
print("The value is: " pyperclip.paste())
while True:
open_browser()