Fechar Janela popup com switch_to_window nao está funcionando

10 views
Skip to first unread message

Danilo Cesar Ribeiro da Silva Lima

unread,
Sep 27, 2017, 12:50:56 PM9/27/17
to Python Brasil

Olá pessoal, estou estudando Python e venho testando alguns comando para navegação em um determinado site e cheguei ao ponto que não estou conseguindo fechar uma janela popup que se abre após eu efetuar o login no site.


Tela após login:






Utilizo o Pycharm

Segue o código:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys


driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')

NOME = (By.ID, "txtLogin")
PASS = (By.ID, "txtSenha")
SUBMIT = (By.CSS_SELECTOR, "input[type=\"button\"]")
MENU1= (By.XPATH, "html/body/div[1]/div[6]")
MENU2 = (By.XPATH, "html/body/div[5]")

driver.get('https://www.comprasnet.gov.br/seguro/loginPortal.asp')

driver.implicitly_wait(5)

selecao = Select(driver.find_element_by_name("perfil"))

selecao_opcao = selecao.options

selecionado = selecao.select_by_index(1)

driver.find_element(*NOME).clear()

login = driver.find_element(*NOME).send_keys("******")

password = driver.find_element(*PASS).send_keys("*******")

botao_submit = driver.find_element(*SUBMIT).click()

#pega o ID da janela atual

window_principal = driver.window_handles[0]
print(window_principal)

window_secundario = driver.window_handles[1]
print(window_secundario)

driver.switch_to_window(window_secundario)

driver.close(window_secundario)



Retorno Debug:

/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 54673 --file /Users/luribeiro/PycharmProjects/scriptspy/cry.py
pydev debugger: process 1550 is connecting

Connected to pydev debugger (build 172.3968.37)
Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/luribeiro/PycharmProjects/scriptspy/cry.py", line 44, in <module>
    window_secundario = driver.window_handles[1]
IndexError: list index out of range

Process finished with exit code 1


Qual o meu erro?

Desde já agradeço.

Abraço.

Danilo Lima









Luciano da Silva Martins

unread,
Sep 27, 2017, 1:44:00 PM9/27/17
to Python Brasil
o Problema com array...está esttourando o array 
Reply all
Reply to author
Forward
0 new messages