1

我有这个 HTML 代码,我正在使用 python 脚本处理/报废:

<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>

这是函数的定义:

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');
}

我需要VerMapa('AVDA. DE AMERICA, 2 -33880 POLA DE ALLANDE','RODRIGUEZ-PELAEZ PEÑA, ANA MARIA','AVDA. DE AMERICA, 2 -33880','33880','ALLANDE','183','178')在 href 属性中调用 javascript 函数并获取返回的 HTML 代码。

我已经检查了蜘蛛猴和 PyV8,但我认为他们在这里帮不了我。蜘蛛猴很清楚地告诉你不能调用 javascript 定义的函数。

我现在正在尝试使用 Selenium,但我不知道如何将源代码返回给 python。

据了解,我刚刚实现了一个测试脚本并设法在浏览器窗口中加载请求的页面。

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.- 我在 htmlCode var 中没有得到任何东西。¿我怎样才能取回由 javascript 函数生成的源代码?我已经从这篇文章中获取语法在 Selenium 中获取 Javascript 代码的返回值

2.- 有没有办法在不打开任何浏览器窗口的情况下使用 Selenium?

4

1 回答 1

0

VerMapa函数没有返回值。这就是为什么您的htmlCode变量不显示任何内容的原因。

要获取新窗口的源,首先调用VerMapa,然后切换到新窗口,然后获取源。

# 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
于 2013-05-31T22:01:47.700 回答