Created
May 16, 2020 17:18
-
-
Save ocvs/a3dee2a72e4fd49b2b8f0aefe1e8c2ed to your computer and use it in GitHub Desktop.
Curso selenium Dunossauro exercicio 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium.webdriver import Firefox | |
from urllib.parse import urlparse | |
from time import sleep | |
def find_by_text(browser, tag, text): | |
"""Encontrar o elemento com o texto `text`. | |
Argumentos: | |
- browser = Instancia do browser [firefox, chrome, ...] | |
- texto = conteúdo que deve estar na tag | |
- tag = tag onde o texto será procurado | |
""" | |
elementos = browser.find_elements_by_tag_name(tag) # lista | |
for elemento in elementos: | |
if text in elemento.text: | |
return elemento | |
def verifica_pagina_com_elementos(elemento='a'): | |
''' | |
aguardar enquanto carrega a link_pagina | |
retorna os elementos com link | |
''' | |
main = '' | |
while not main: | |
main = browser.find_element_by_tag_name('main') | |
print('main presente') | |
elementos='' | |
while not elementos: | |
elementos = main.find_elements_by_tag_name(elemento) | |
return elementos | |
browser = Firefox() | |
browser.get('http://selenium.dunossauro.live/exercicio_03.html') | |
print('Estou na pagina inicial do exercício. Começar a jogar') | |
texto = 'Começar' | |
elemento_com_link = '' | |
while not elemento_com_link: | |
elemento_com_link=find_by_text(browser, 'a', texto) | |
link_pagina1=elemento_com_link.get_attribute('href') | |
elemento_com_link.click() | |
print('Foi para página page_2.html') | |
sleep(3) | |
a_elementos = verifica_pagina_com_elementos('a') | |
for a in a_elementos: | |
if 'errado' == a.get_attribute('attr'): | |
a.click() | |
break | |
print('Foi para penultima etapa') | |
sleep(3) | |
a_elementos = verifica_pagina_com_elementos('a') | |
a_resposta = find_by_text(browser, 'a', browser.title) | |
a_resposta.click() | |
print('clicou resposta com titulo da pagina') | |
a_elementos = verifica_pagina_com_elementos('a') | |
path = urlparse(browser.current_url).path.replace('/','') | |
print(path) | |
a_resposta = find_by_text(browser, 'a', path) | |
a_resposta.click() | |
print('clicou resposta com path pagina') | |
p_elementos = verifica_pagina_com_elementos('p') | |
for p in p_elementos: | |
if 'refresh' in p.text: | |
print('foi para ultima etapa - diabão aparece - dar refresh na pagina') | |
browser.refresh() | |
print('unicornio') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment