Python Selenium Webdriver语言 - 查找嵌套框架



我正在使用嵌套框架的内联网,无法访问子框架。

网页源代码:

<!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()

我尝试了以下代码来定位输入 id="year" 在 frame="Formulario" 中:

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"]"))

但它们不起作用。

你可以帮我吗?

谢谢!

为了能够处理所需的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() 这样的方法,switch_to_default_content()方法已被弃用,因此您最好使用 switch_to.frame()switch_to.default_content()

假设您的程序专注于顶级浏览上下文,要找到带有文本的按钮作为 Buscar,您需要switch()遍历所有父以及与适当的expected_conditions相关联的 WebDriverWait,您可以使用以下代码块:

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

最新更新