1

I have this HTML code I'm proccesing/scrapping with a python script:

<a href="javascript:VerMapa('AVDA. DE AMERICA, 2  -33880 POLA DE ALLANDE','RODRIGUEZ-PELAEZ PEÑA, ANA MARIA','AVDA. DE AMERICA, 2  -33880','33880','ALLANDE','183','178')"><img src="../../Fijos/Img/VerMapa.jpg" alt="" width="25" height="24" border="0">&nbsp;<br>Ver Mapa</a>

This is the definition of the function:

function VerMapa(direccion,farmacia,domicilio,CP,municipio, numeroOficina,identidad) {
    window.open("googleMaps.aspDir="+direccion+"&Far="+farmacia+"&Dom="+domicilio+"&CP="+CP+"&Mun="+municipio+"&NO="+numeroOficina+"&Id="+identidad+"&",'Lista','toolbar=no, menubar=no, scrollbars=yes,resizable=yes,status=yes,screenX=50,top=50,width=740,height=530');
}

I need to make a call to VerMapa('AVDA. DE AMERICA, 2 -33880 POLA DE ALLANDE','RODRIGUEZ-PELAEZ PEÑA, ANA MARIA','AVDA. DE AMERICA, 2 -33880','33880','ALLANDE','183','178') javascript function in the href attribute and get the HTML code returned.

I have checked spider-monkey and PyV8 but I think they can't help me here. spider-monkey is clear telling you can´t call a javascript defined function.

I'm now trying with Selenium, but I don't know how I can get the source code back to python.

By know I just have implemented a test script and managed to load the requested page in a browser window.

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.farmasturias.org/GESCOF/cms/Guardias/FarmaciaBuscar.asp?IdMenu=111")
htmlCode = browser.execute_script("return VerMapa('AVDA. DE AMERICA, 2  -33880 POLA DE ALLANDE','RODRIGUEZ-PELAEZ PEÑA, ANA MARIA','AVDA. DE AMERICA, 2  -33880','33880','ALLANDE','183','178')")
print htmlCode;

1.- I am not getting anything in htmlCode var. ¿How can I get back the source code generated by the javascript function? I have taken the syntax from this post Getting the return value of Javascript code in Selenium

2.- Is there any way to use Selenium without opening any browser window?

Community
  • 1
  • 1
David Casillas
  • 1,801
  • 1
  • 29
  • 57
  • I have tried the same code with java and a new pop up window is populated. After executing javascript and using print htmlCode, do you need the html source of that pop up window where map is shown? – HemChe Apr 24 '13 at 10:44
  • Maybe I make not it clear. The code pops up a new window with the expected content, but nothing is returned in htmlCode var. I'm trying to get in that var the source code of the page. – David Casillas Apr 24 '13 at 11:42
  • @DavidCasillas Do you want the source code of the main page or the newly opened pop-up window? – HemChe Apr 25 '13 at 08:18
  • The on created for the pop-up window. – David Casillas Apr 25 '13 at 18:24

1 Answers1

0

The VerMapa function doesn't have a return. That's why your htmlCode variable doesn't show anything.

To get the source of the new window, first call VerMapa, then switch to the new window, then get the source.

# get all open windows
handles = set(broswer.window_handles)

# remove the current window from the set
handles.remove(browser.current_window_handle)

# switch to the other window
browser.switch_to_window(handles.pop())

# get the source of that window
htmlCode = browser.page_source
Dingredient
  • 2,191
  • 22
  • 47