【问题标题】:Webdriver cannot locate correct xpath for dxheViewAreaWebdriver 无法为 dxheViewArea 找到正确的 xpath
【发布时间】:2014-06-21 16:21:06
【问题描述】:

我正在自动化的页面有一个 dxheViewArea。我想在这个字段中输入一些文字。

我的 Webdriver 代码是:

d.FindElement(By.CssSelector(".dxheDesignViewArea dxheViewArea")).SendKeys("Who invented the first fixed witn aircraft?"); 

我在 NUnit 中运行,返回的错误是 Unable to locate element:{"method":"css selector","selector":".dxheDesignViewArea dxheViewArea"}

在 Firefox 中我检查元素,代码是:

<body class="dxheDesignViewArea dxheViewArea" style="border-width: 0px;" spellcheck="false"/>

请问我可以使用什么 Xpath 语法来定位并在此元素中输入一些文本?

完整来源在这里:

        <style/>
        <link href="/DXR.axd?r=4_1" type="text/css" rel="stylesheet"/>
        <style charset="utf-8" type="text/css">xxxxxxxx</style>
        <style id="firepath-matching-node-style" type="text/css">.firepath-matching-node      { outline: 2px dashed #00F;}</style>
    </head>
    <body class="dxheDesignViewArea dxheViewArea" style="border-width: 0px;"             spellcheck="false"/>
</html>

【问题讨论】:

    标签: c# xpath selenium-webdriver


    【解决方案1】:

    您应该将这两个 CSS 类都限定为类。你所做的只是第一个。试试这个:

    d.FindElement(By.CssSelector(".dxheDesignViewArea.dxheViewArea")).SendKeys("Who invented the first fixed witn aircraft?");
    

    【讨论】:

      【解决方案2】:

      我认为您应该找到包含指定字段的 WebElement:

      WebElement container = driver.findElement(By....); // locate the WebElement that contains the input field.... by cssselector or xpath
      container.findElement(By.xpath("//*[@class='dxheDesignViewArea dxheViewArea']")).SendKeys("Who invented the first fixed witn aircraft?");
      

      更新:没有完整的 html 代码并不容易,但我找到了 driver.switchTo() 方法:

      WebElement iframe = driver.findElement(By.xpath("//*[@id='...']")) // locate the iframe
      driver.switchTo().frame(iframe);
      .... // find elements
      driver.switchTo().defaultContent();
      

      参考资料:

      【讨论】:

        【解决方案3】:

        试试这个

        driver.findElement(By.className("dxheDesignViewArea dxheViewArea")).sendKeys("Who invented the first fixed witn aircraft?");
        

        【讨论】:

        • 那行不通。我想这是因为它在一张桌子里面。我设法检查了它周围的元素,源代码如下所示:
        • 还有另外一张表:
        • 现在很混乱,嘿
        • 因为 iframe 我更新了我的答案......我希望它会有用
        【解决方案4】:

        我一直在尝试这些建议,但仍然没有成功。 我检查了更多代码并注意到有一个 iFrame。 这是因为 iFrame 无法定位元素。

        下面是代码sn-p。我现在可以使用什么 Xpath 来定位 class="dxheDesignViewArea dxheViewArea"

        <iframe id="ctl00_uxContentPlaceHolderContent_uxQuestionHTMLTextEditor_uxQuestionHTMLTextEditor_editor_DesignIFrame" class="dxheDesignViewArea dxheViewArea" frameborder="0" style="height: 100%; width: 100%; padding: 0px; border-style: none; border-width: 0px;" src="javascript:false" name="ctl00_uxContentPlaceHolderContent_uxQuestionHTMLTextEditor_uxQuestionHTMLTextEditor_editor_DesignIFrame">
        

        【讨论】:

          【解决方案5】:

          我现在终于设法让它工作了。它在 iFrame 中。这是我使用的代码。

          IWebElement iframe = driver.FindElement(By.Id("ctl00_uxContentPlaceHolderContent_uxQuestionHTMLTextEditor_uxQuestionHTMLTextEditor_editor_DesignIFrame"));
                  driver.SwitchTo().Frame(iframe);
                  driver.FindElement(By.XPath("//body[@class='dxheDesignViewArea dxheViewArea']")).SendKeys("Who invented the first fixed wing aircraft?");
                  driver.SwitchTo().DefaultContent(); 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-11-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-11-04
            相关资源
            最近更新 更多