Issue
guys!
How can I catch the text from a javascript alert and set a OK using chrome headless? The follow code works very well, but if I set headless option it seems not find the pop up alert.
Edit I realize that my code stops on " self.action.move_to_element(firstLevelMenu).perform() ", instead on javascript alert like I thought. How can I fix it?
import os from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.firefox import GeckoDriverManager from selenium.webdriver.common.keys import Keys import time as pausa from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC #olhar from selenium.webdriver.remote.webelement import WebElement from datetime import datetime from tkinter import * from tkinter import scrolledtext, messagebox, ttk import pyautogui as p import csv import datetime from datetime import date, datetime import pyodbc
class Driver:
def __init__ (self):
if nav_cbx.get()=='Chrome':
options = webdriver.ChromeOptions()
options.add_experimental_option('useAutomationExtension', False)
# options.add_argument("--headless")
options.add_argument("--dns-prefetch-disable")
options.add_argument("--disable-infobars")
self.driver = webdriver.Chrome('c:/driver/chromedriver.exe', options=options) #PASTA DO SCRIPT
self.action = ActionChains(self.driver)
self.wait = WebDriverWait(self.driver, 30)
self.wait2 = WebDriverWait(self.driver, 10)
agora = str(datetime.today()).replace(':', '.')
cria_pasta_log()
self.log = open(f'{caminho_log()}/log{agora}.csv', "w", newline='\n', encoding='ANSI')
self.log.write(f'ID;Chpras;Bin;Final;Portador;Status')
self.login = in_login.get()
self.senha = in_senha.get()
def menu_reemitir(self, chpras):
self.retorna_frame()
firstLevelMenu = self.driver.find_element(By.XPATH, '//*[@id="imgMenu"]')
self.action.move_to_element(firstLevelMenu).perform() #menu opções
secondLevelMenu = self.driver.find_element(By.XPATH, '//*[@id="elem9"]')
secondLevelMenu.click() #submenu reemitir cartão
self.driver.switch_to.window(self.driver.window_handles[2]) #alternar para popup
self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="FormSAT"]/div[2]/table[2]/tbody/tr/td[1]/input'))).click()
# self.driver.execute_script("modificarReemision()")
pausa.sleep(0.5)
#### THE PROBLEM STARTS RIGHT HERE!!!!!!!!!!!!!!!!!!!!!!!!
#there's 2 alerts. On the first one I need set an accept
alert1 = self.wait.until(EC.alert_is_present())
alert1.accept()
pausa.sleep(0.5)
try:
# on the second one I need cacth the text and set another "ok"
alert2 = self.wait.until(EC.alert_is_present())
text = alert2.text
alert2.accept()
print(text)
if 'BLOQU' in str(text).upper():
text = self.captura_msg_bloqueio(chpras)
self.log.write(f'{text};')
except:
self.log.write(f'Não foi possível capturar a mensagem de retorno;')
self.driver.close()
self.aceita_alerta()
self.driver.switch_to.window(self.driver.window_handles[1])
pausa.sleep(0.5)
I really need use the headless option, and I need load/accept the both pop up alerts. How I do this?
Solution
You can overwrite alert to catch the message:
driver.executeScript("""
window.alert = message => window.alertMessage = message
""")
Then get it later
message = driver.executeScript("return window.alertMessage")
Answered By - pguardiario
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.