Selenium Python

1

I'm having trouble manipulating popup. It appears that 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 quite capable of having it too.

  

QUESTION: My goal is to create a crawler to take data from several   invoices per month, and thus control the company's expenses.

Follow the 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 can not manipulate the form field that appears in the popup (or will it be window?) And thus insert the license plate. I already used the:

driver.current_window_handle 
driver.window_handles

The latter only points to an id. I've 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 
    
asked by anonymous 08.12.2015 / 18:02

2 answers

2

This popup is not a real popup. Actually it's just a div with position: absolute . It's a bit tricky to access that input because it's inside an iframe.

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

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 driver.switch_to.frame() method, but we would be wasting time loading the parent frame, and we do not want anything with it.

    
14.12.2015 / 03:08
0

I think that's what you want to do, but if you're not testing the link, follow the rodorgas 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)
    
02.02.2017 / 15:07