Selenium无法在带有开关框架的IE中找到元素

时间:2018-10-04 14:19:06

标签: javascript selenium-webdriver

我正在硒中创建一个仅在IE中运行的进程,因此无法找到xpath。

我能够使用切换框推进测试,

切换到框架“ frame1 ”或尝试确定发生错误的元素( id:txtChassi ):

线程“主” org.openqa.selenium.NoSuchFrameException中的异常:名称或ID为frame1的框架元素均未找到

线程“ main”中的异常org.openqa.selenium.NoSuchElementException:无法使用css选择器== #txtChassi

查找元素

尝试一次:

driver0.switchTo().frame("frame1");

WebElement IptBoxChassi = driver0.findElement(By.id("txtChassi"));
IptBoxChassi.sendKeys(rs.getString(cChassi));

尝试两次:

WebDriverWait waitIptBoxChassi = new WebDriverWait(driver0, 10);                        

waitIptBoxChassi.until(ExpectedConditions.visibilityOfElementLocated(By.id("txtChassi")));

 WebElement IptBoxChassi = driver0.findElement(By.id("txtChassi"));
    IptBoxChassi.sendKeys(rs.getString(cChassi));

这里有来自Error和WebPage的更多数据:

enter image description here

<FRAME noResize marginHeight=0 src="tela_topo.asp" frameBorder=no name=top marginWidth=0 scrolling=no target="contents">

enter image description here

<FRAMESET id=frame1 border=0 cols="157,*,0'" onunload="javascript: if (!(fechando)) window.location.href = '/controleacesso/encerrar.asp';"><FRAME noResize marginHeight=0 src="pre_menu.asp" frameBorder=no name=contents marginWidth=0 scrolling=no target="form"><FRAMESET id=frameAbaixo border=0 rows=*%,17%,0% onunload="javascript: if (!(fechando)) window.location.href = '/controleacesso/encerrar.asp';"><FRAME noResize marginHeight=0 src="tela_direita.asp" frameBorder=no name=form marginWidth=0><FRAME noResize marginHeight=0 src="pre_botoes.asp" frameBorder=no name=botoes marginWidth=0 scrolling=no><FRAME noResize marginHeight=0 src="Home/Home.asp" frameBorder=no name=Consulta marginWidth=0></FRAMESET><FRAMESET id=framelado border=5 rows=*,17% onunload="javascript: if (!(fechando)) window.location.href = '/controleacesso/encerrar.asp';"><FRAME noResize marginHeight=0 src="tela_direita.asp" frameBorder=no name=formlado marginWidth=0><FRAME noResize marginHeight=0 src="pre_botoes.asp" frameBorder=no name=botoeslado marginWidth=0 scrolling=no></FRAMESET></FRAMESET>

enter image description here

<FRAME noResize marginHeight=0 src="pre_menu.asp" frameBorder=no name=contents marginWidth=0 scrolling=no target="form">

这是发生错误的地方,WebDriver在框架中看不到该元素

enter image description here

<FORM id=form1 method=post name=form1 action=RegistroContrato.asp><INPUT id=oculto type=hidden name=oculto> <INPUT type=hidden name=idPessoa> <INPUT id=ocultoIdTransacao type=hidden name=ocultoIdTransacao> <INPUT id=ocultoIdAgenteFinanceiro type=hidden name=ocultoIdAgenteFinanceiro> <INPUT id=ocultoCNPJCredor type=hidden name=ocultoCNPJCredor> <INPUT id=ocultoNomeCredor type=hidden name=ocultoNomeCredor> <INPUT id=ocultoCPFCNPJDevedor type=hidden name=ocultoCPFCNPJDevedor> <INPUT id=ocultoNomeDevedor type=hidden name=ocultoNomeDevedor> <INPUT id=ocultoSequencia type=hidden name=ocultoSequencia> <INPUT id=ocultoPlaca type=hidden name=ocultoPlaca> <INPUT id=ocultoRenavam type=hidden name=ocultoRenavam> <INPUT id=ocultoUF type=hidden name=ocultoUF> <INPUT id=ocultoMarcaModelo type=hidden name=ocultoMarcaModelo> <INPUT id=ocultoAnoFabricacao type=hidden name=ocultoAnoFabricacao> <INPUT id=ocultoAnoModelo type=hidden name=ocultoAnoModelo> <INPUT id=ocultoEspecie type=hidden name=ocultoEspecie> <INPUT id=ocultoTipoVeiculo type=hidden name=ocultoTipoVeiculo> <INPUT id=ocultoCombustivel type=hidden name=ocultoCombustivel> <INPUT id=ocultoCategoria type=hidden name=ocultoCategoria> <INPUT id=ocultoDataContrato type=hidden name=ocultoDataContrato> <INPUT id=ocultoCodigoTipoContrato type=hidden name=ocultoCodigoTipoContrato> <INPUT id=ocultoDescricaoTipoContrato type=hidden name=ocultoDescricaoTipoContrato> <INPUT id=ocultoStatusContrato type=hidden name=ocultoStatusContrato> 
<TABLE cellSpacing=1 cellPadding=0 width="90%" align=center border=0>
<TBODY>
<TR>
<TD width="15%">Chassi:&nbsp;</TD>
<TD width="85%"><INPUT onkeypress=javascript:ValidaCaracter(); id=txtChassi class=box maxLength=20 size=30 name=txtChassi> </TD></TR>
<TR>
<TD width="15%">Contrato:&nbsp;</TD>
<TD width="85%"><INPUT id=txtNumeroContrato class=box maxLength=20 size=30 name=txtNumeroContrato> </TD></TR>
<TR>
<TD colSpan=2 noWrap>
<HR color=#376cb7 SIZE=1>
</TD></TR></TBODY></TABLE></FORM>

1 个答案:

答案 0 :(得分:0)

错误1:

  1. 请确保您的元素位于iFrame中,然后只需切换到所需的iframe并在所需的元素上执行操作即可。

  2. 如果您早些时候已切换到某些iFrame,则不要忘记从该iframe切换回(一旦完成工作)即可与普通网页上的其他元素一起使用。

例如

  • 按帧切换Web元素

    WebElement frame = driver.FindElement(By.locator);
    driver.switchTo().frame(frame);
    
  • 使用索引切换

    driver.switchTo().frame(0); // frame index 0, 1 and so on
    
  • 使用ExplicitWait进行切换(将等待直到给定的帧可用,一旦可用就重新切换)

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.locator));
    
    ....
    
    //perform action on webelement inside iframe
    
    ....
    
    driver.switchTo().defaultContent();  // switchback from frame
    
    /* if there is any other iframe and you have to work within then again you have to switch into other iframe as well as shown above*/
    

错误2:

  1. 如果您的元素不在iframe中(如果元素在iframe中,则显示NoSuchElementException),然后使用隐式或显式代码,直到该元素可用于执行操作为止。

次要观察:这是说无法使用css选择器== #txtChassi 查找元素,但是您正在使用By.id