Skip to content

Instantly share code, notes, and snippets.

@eliasalbuquerque
Last active January 15, 2024 11:58
Show Gist options
  • Save eliasalbuquerque/7c9ce2e1722274e78668fe6f4694061f to your computer and use it in GitHub Desktop.
Save eliasalbuquerque/7c9ce2e1722274e78668fe6f4694061f to your computer and use it in GitHub Desktop.
Curso Dev Aprender | Jhonatan de Souza

Encontrar elementos desabilitados no site

Código:

# 202401 - Python 3.12.0
# MA_5.19 - Como saber se um elemento está habilitado


import logging
from app import iniciar_driver
from selenium.webdriver.common.by import By


def elemento_habilitado():
    logger = logging.getLogger(__name__)
    try:
        # args:
        # site = 'https://site/'
        # detach = True
        # sleep_mode = True
        # zoom_level=.75
        site = 'https://cursoautomacao.netlify.app/'
        driver = iniciar_driver(site_url=site, detach=True, zoom_level=.75)

        # acessa o iframe do elemento para poder acessar o elemento:
        driver.switch_to.frame(driver.find_element(By.XPATH, "//iframe[@src='https://cursoautomacao.netlify.app/desafios.html']"))

        # busca os elementos pelo elemento pai:
        botoes = driver.find_elements(
            By.XPATH, "//section[@class='jumbotron desafios1']/button")

        # depois itera sobre os elementos filhos:
        for botao in botoes:
            if botao.is_enabled():
                print(f'Botão habilitado: {botao.text}.')
            else:
                print(f'{botao.text} esta desabilitado!')

        # fecha o navegador
        driver.close()

    except Exception as e:
        logger.error(f'Ao tentar verificar elementos habilitados\n- {type(e).__name__}: {str(e)}')


elemento_habilitado()

Terminal:

PS C:\Users\elias\Workspace\python-selenium> python .\07_desafio_elementos_desabilitados.py

DevTools listening on ws://127.0.0.1:62584/devtools/browser/1f945456-5b82-4c6d-bd8f-c2806c5c4e58
Botão 1 esta desabilitado!
Botão habilitado: Botão 2.
Botão habilitado: Botão 3.
PS C:\Users\elias\Workspace\python-selenium>

Rodando a aplicação:

elementos-habilitados


GitHub: eliasalbuquerque/python-selenium

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment