Python Selenium Webdriver - 查找嵌套框架

时间:2018-03-11 15:01:56

标签: python selenium webdriver

我正在使用嵌套框架的Intranet,但无法访问子框架。

HTML源代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
		<title>VIS</title>
		<link rel="shortcut icon" href="https://bbbbb/ma1/imagenes/iconos/favicon.ico">
	</head>
	<frameset rows="51,*" frameborder="no" scrolling="no" border="0">
		<frame id="cabecera" name="cabecera" src="./blablabla.html" scrolling="no" border="3">
			<frameset id="frame2" name="frame2" cols="180,*,0" frameborder="no" border="1">
				<frame id="menu" name="menu" src="./blablabla_files/Menu.html" marginwidth="5" scrolling="auto" frameborder="3">
					<a href="/ma1/jsp/orD/queda.jsp" target="contenido">Buscar</a>
				<frame id="contenido" name="contenido" src="./blablabla_files/saved_resource.html" marginwidth="5" marginheight="5">
					<html>
						<head>
							<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
							<title>BUSCAr</title>
						</head>
						<frameset name="principal" rows="220,*" frameborder="NO">
							 <frame name="Formulario" src="./BusquedaSimple.html" scrolling="AUTO" noresize="noresize">
								<input id="year" name="year" size="4" maxlength="4" value="" onchange="javascript:Orden();" onfocus="this.value='2018';this.select();" type="text">
							 <frame name="Busqueda" src="./saved_resource(2).html" scrolling="AUTO">
						</frameset>
						<noframes>
							&lt;body&gt;
							&lt;p&gt;soporte a tramas.&lt;/p&gt;
							&lt;/body&gt;
						</noframes>
					</html>
				<frame name="frameblank" marginwidth="0" scrolling="no" src="./blablabla_files/saved_resource(1).html">
			</frameset>
			<noframes>
			  &lt;P&gt;Para ver esta página.&lt;/P&gt;
			</noframes>
	</frameset>
</html>

我在框架“菜单”中找到了“Buscar”按钮:

driver.switch_to_default_content()
driver.switch_to_frame(driver.find_element_by_css_selector("html frameset frameset#frame2 frame#menu"))
btn_buscar = driver.find_element_by_css_selector("#div_menu > table:nth-child(10) > tbody > tr > td:nth-child(2) > span > a")
btn_buscar.click()

我已尝试使用此代码在frame =“Formulario”中找到输入id =“year”:

driver.switch_to_default_content()
try:       driver.switch_to_frame(driver.switch_to_frame(driver.find_element_by_css_selector("html frameset frameset#frame2 frame#contenido frameset#principal frame#Formulario")))
    print("Ok cabecera -> contenido")
except:
    print("cabecera not found")

driver.switch_to_frame(driver.switch_to_xpath("//*[@id='year"]"))

但它们不起作用。

你能帮助我吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

为了能够处理所需的iframe,您需要随后切换到所有 祖先帧:

driver.switch_to.frame("cabecera")
driver.switch_to.frame("menu")
btn_buscar = driver.find_element_by_link_text("Buscar")
btn_buscar.click()

另请注意,Webdriver实例没有switch_to_xpath()switch_to_frame()方法,{@ 1}}方法已被弃用,因此您最好使用switch_to_default_content(),{{1 }}

答案 1 :(得分:0)

假设您的计划专注于Top Level Browsing Context,要找到文本为 Buscar 的按钮,您需要switch()通过所有父框架以及WebDriverWait与正确的expected_conditions相关联,您可以使用以下代码块:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"cabecera"))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"menu"))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Buscar"))).click()