加载具有相同名称的框架后无法找到Web元素

时间:2013-08-30 11:35:32

标签: ruby ajax watir-webdriver

我对以下代码提出了一些具体问题:

require 'rubygems'
require "watir-webdriver"

webpage = "http://www.portalinmobiliario.com/catalogo/fichas.asp?ProyectoID=4308&tp=1&op=1&iug=306&ca=1&ts=1&mn=2&or=&sf=1&sp=1"
pag_detalle = Watir::Browser.new :firefox
pag_detalle.goto(webpage)

if pag_detalle.frame(:id => 'iFrameFicha').table(:id => 'TableInformacionBasicaProyecto').exists? then

    pag_detalle.frame(:id => 'iFrameFicha').table(:id => 'TableInformacionBasicaProyecto').link(:id => 'btnCotizar').when_present.click

    sleep 5

    if pag_detalle.frame(:id => 'iFrameFicha').table(:id => 'Cotizar').exists? then
        puts "existe"
    end

    pag_detalle.close       
end

代码打开firefox并加载页面。然后单击“Cotizar”按钮。之后,框架'iFrameFicha'会更改其内容,但无法访问其元素。

错误消息表明我应该切换到容器框架,但我无法。

1 个答案:

答案 0 :(得分:1)

您看到的错误消息似乎是watir-webdriver(或selenium-webdriver)中的错误。从快速测试开始,只要尝试访问框架中的任何内容并且该元素不存在,就会抛出异常。我相信这与Issue 211相同。我认为,问题的例外是不同的,只是因为它使用的是Chrome(即如果你使用firefox来解决问题,你会得到例外)。

特别是,当您执行该行时:

if pag_detalle.frame(:id => 'iFrameFicha').table(:id => 'Cotizar').exists? then

该元素不存在,如上所述(错误地)抛出异常。

当我查看页面时,有3个表,其中没有一个具有id。

我猜你真的想要带有“Cotizar”的表:

<table class="Cotizar" border="0" width="100%">

这意味着代码应该是:

if pag_detalle.frame(:id => 'iFrameFicha').table(:class=> 'Cotizar').exists? then

但也许你想要body元素的id为“cotizar”(注意小写)。

<body onload="resize();ExisteMarco();" style="margin:0px;" id="cotizar" class="pageBtn">

在这种情况下,你需要这样做:

if pag_detalle.frame(:id => 'iFrameFicha').body(:id => 'cotizar').exists? then