Selenium Python

Asked

Viewed 398 times

2

I’m having trouble manipulating popup. Apparently, the popup code is generated in real time, not being captured by Selenium. In fact, it is possible to see the use of jQuery, I did not detect Ajax, but very capable that has also.

QUESTION: My goal is to create a Crawler to take data from various invoices per month, and thus to control the company’s expenses.

Follows code below:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Firefox()
driver.get("http://servicos.coelba.com.br/Pages/Default.aspx")

link = driver.find_element_by_id("ctl00_m_g_bf2869f9_4567_4bf8_bee4_c5c7c5b50e2e_ctl00_rptAplicacoes_ctl02_lnkAplicacao")
link.click()

contratoFieldName = "numcontacontrato" 
contratoFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_name(contratoFieldName))

The problem occurs in the last two lines. I cannot manipulate the form field that appears in the popup (or is it window?) and so enter the plate. I’ve used the:

driver.current_window_handle 
driver.window_handles

the latter only points to an id. I have tried the following switches:

driver.switch_to                 
driver.switch_to_alert            
driver.switch_to_frame
driver.switch_to_active_element  
driver.switch_to_default_content  
driver.switch_to_window 

2 answers

2

This popup is not a real popup. It’s actually just a div with position: absolute. It’s a little complicated to access that input because it’s inside an iframe.

The best thing to do in this case is to directly load the iframe page:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Firefox()
driver.get('http://autoatendimento.coelba.com.br/NDP_DCSRUCES_D~home~neologw~sap.com/login.jsp?canal=hotsite&dest=26')

matricula = '0123456'
driver.find_element_by_name('numcontacontrato').send_keys(matricula)

You can even access the iframe with the method driver.switch_to.frame(), but we’d be wasting time carrying the parent frame, since we don’t want anything to do with it.

0

I think that’s what you want to do, but if you’re not going to test the link, follow the route approach.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
driver = webdriver.Firefox()
driver.get("http://servicos.coelba.com.br/Pages/Default.aspx")

link = driver.find_element_by_id("ctl00_m_g_bf2869f9_4567_4bf8_bee4_c5c7c5b50e2e_ctl00_rptAplicacoes_ctl00_lnkAplicacao")
link.click()

sleep(20) # Aqui você tem que mapear o load e esperar ele desaparecer, da pra fazer dinamicamente.

# Frame Pai
driver.switch_to.frame(0)
# Frame Filho
driver.switch_to.frame(0)
contratoFieldElement = driver.find_element_by_id("txtContractAccount")
contratoFieldElement.send_keys('1234567')

contratoFieldElement = driver.find_element_by_id("txtContractAccount")
contrato_digitado = contratoFieldElement.get_attribute('value')

print(contrato_digitado)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.