Selenium 1 - 切换到没有ID

时间:2015-09-21 10:46:02

标签: selenium iframe

我绉的页面已从iframe中删除了ID,所以我在切换到iframe时出现问题,而且我找不到任何文档可以帮助我,所以也许堆栈上有人?

网页的网址为:http://www.klappen.se/boka/onlinebokning/

我使用的是Selenium 1,我的代码如下所示:

$this->_driver->switchTo()->getFrameByName("mainframe");

在我的TargetLocator.php中,我有以下功能:

<?php
// Copyright 2012-present Nearsoft, Inc

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace SeleniumClient;

require_once 'WebDriver.php';

class TargetLocator
{
    private $_driver;

    public function __construct(WebDriver $driver)
    {
        $this->_driver = $driver;
    }

    #region TargetLocator members
    /**
     * Move to a different frame using its index
     * @param Integer $frameIndex
     * @return current WebDriver
     */
    public function getFrameByIndex($frameIndex)
    {

        $this->_driver->getFrame($frameIndex);

        return $this->_driver;
    }

    /**
     * Move to different frame using its name
     * @param String $frameName
     * @return current WebDriver
     */
    public function getFrameByName($frameName)
    {
        //We should validate that frameName is string
        /*
        if ($frameName == null)
        {
            throw new ArgumentNullException("frameName", "Frame name cannot be null");
        }
        */

        $this->_driver->getFrame($frameName);

        return $this->_driver;
    }

    /**
     * Move to a frame element.
     * @param WebElement $frameElement
     * @return current WebDriver
     */
    public function getFrameByWebElement(WebElement $frameElement)
    {
        //We should validate that frameElement is string
        /*
        if (frameElement == null)
        {
            throw new ArgumentNullException("frameElement", "Frame element cannot be null");
        }

        RemoteWebElement convertedElement = frameElement as RemoteWebElement;
        if (convertedElement == null)
        {
            throw new ArgumentException("frameElement cannot be converted to RemoteWebElement", "frameElement");
        }
        */

        $frameId = $frameElement->getElementId();
        $target = array('ELEMENT' => $frameId);
        $this->_driver->getFrame($target);

        return $this->_driver;
    }

    /**
     * Change to the Window by passing in the name
     * @param String $windowName
     * @return current WebDriver
     */
    public function getWindow($windowName)
    {
        $this->_driver->getWindow($windowName);

        return $this->_driver;
    }

    /**
     * Change the active frame to the default
     * @return current WebDriver
     */
    public function getDefaultFrame()
    {
        $this->_driver->getFrame(null);

        return $this->_driver;
    }

    /**
     * Finds the active element on the page and returns it
     * @return WebElement
     */
    public function getActiveElement()
    {
        $webElement = null;

        $webElement = $this->_driver->getActiveElement();

        return $webElement;
    }


    /**
     *  Switches to the currently active modal dialog for this particular driver instance.
     * @return \SeleniumClient\Alert
     */
    public function getAlert()
    {
        // N.B. We only execute the GetAlertText command to be able to throw
        // a NoAlertPresentException if there is no alert found.
        //$this->_driver->getAlertText();
        return new Alert($this->_driver); //validate that the Alert object can be created, if not throw an exception, try to use a factory singleton o depency of injection to only use 1 instance
    }
    #endregion
}

我已经尝试了所有这些,但无法让它发挥作用。有没有人可以提供帮助: - )?

提前致谢。

2 个答案:

答案 0 :(得分:0)

据我所知,您正在寻找这个iFrame:

<iframe src="http://dlbookit3.dlsystems.se/dlbookitKSR/bmlogifilt/logifilt.aspx" style="height:700px; width:100%; border:0;"></iframe>

正确?所以你有getFrameByWebElement(WebElement)这样的方法接受WebElement。我认为您可以使用xpath来查找webElement例如:

WebElement element = find(By.xpath("//iframe"));
getFrameByWebElement(element);

到目前为止理论上这可行(这是Java,你必须根据你的php代码进行调整)。但是,如果我使用chrome分析页面的HTML代码,则无法使用xpath webElement找到//iframe

仍然可以尝试...但看起来页面所有者不希望其iFrame可以找到: - )

答案 1 :(得分:0)

页面上只有1 IFRAME,因此您只需按标记名称即可找到它。页面加载速度非常慢,但我在美国,所以可能与它有关。您可能必须等待IFRAME变为可用,然后才能获得它。