How to get base64 image through selenium?

Hello, I need to get the base64 from a captcha image to pass it to a method that solves it. The thing is, how do I manage to do that?

This my view but the code with the screenshot variable is the part that does not work:

        DRIVER_PATH = 'C:/Users/python/chromedriver.exe'
        driver = webdriver.Chrome(executable_path=DRIVER_PATH)
        driver.get('https://seti.afip.gob.ar/padron-puc-constancia-internet/ConsultaConstanciaAction.do')

        driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
        wait = WebDriverWait(driver, 30) 
        elem = wait.until(EC.presence_of_element_located((By.XPATH,"//input[@id='cuit']")))
        elem.send_keys(cuit_r)
        
        elem2= wait.until(EC.presence_of_element_located((By.XPATH,"//input[@id='CaptchaCode']")))
        screenshot = driver.get_screenshot_as_base64()

        resolve_simple_captcha(screenshot)
        elem4 = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="btnConsultar"]')))
        elem4.click()

Basically I have to pass the base64 image to my resolve_simple_captcha method in order to get it solved. How do I do that? This is how the method works:

resolve_simple_captcha(image_base64)
1 Like

@micaelagimenez Selenium’s Python bindings have a method for this screenshot_as_base64.

my_image = driver.find_element_by_id('captcha-image-id')
my_base64 = my_image.screenshot_as_base64()
resolve_simple_captcha(my_base64)

Thank you! I understand that should work but I get this error:

TypeError: 'str' object is not callable

Is there another way to do it or a way to fix it?

That error is coming from somewhere else in your code, or the definition of the resolve function.

It says it comes from here: my_base64 = elem2.screenshot_as_base64()

The function I’m supposed to use gets the base64 image and then returns it in a string format if there is one, maybe it has to do with that?

You can remove the curly braces as it is not callable. So, it should look like this:
my_base64 = my_image.screenshot_as_base64