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: