【问题标题】:Unable to locate the elements in SmartGWT application using Selenium无法使用 Selenium 在 SmartGWT 应用程序中定位元素
【发布时间】:2018-02-14 12:08:47
【问题描述】:

我是 Selenium 和 GWT 的初学者,我正在尝试测试一个 GWT Web 应用程序,例如添加了 SmartGWT 库的 Paint。我在尝试使用与所有浏览器兼容的单一方法定位网页元素时遇到问题。我尝试了许多定位元素的方法,但没有一个奏效。首先,我尝试使用无效的 ID 定位元素。然后,我尝试了绝对 XPath 的方法,该方法不适用于不同浏览器上的一些元素。然后,我尝试使用相对 XPath 定位这些元素,但不幸的是,该方法也出现了同样的问题。然后,我找到了另一种使用 scLocators 定位这些元素的方法,如以下链接所示:Using Selenium in SmartGWT

我能够使用此链接中提到的步骤在 Selenium IDE 中生成 scLocators。但是当我停止录制后在 Selenium IDE 中播放整个测试用例时,IDE 本身无法找到那些在我执行各种操作(如单击、编写文本等)时生成的元素。

此外,我还使用 scLocators 在 Java 中使用 Selenium WebDriver 来查找这些元素。但同样,它不起作用,并且没有显示任何此类元素异常。

这是我的代码。

public class RelativeXpath {

public static void main(String[] args) throws InterruptedException {
    System.setProperty("webdriver.chrome.driver", "D:\\SELENIUM\\Drivers\\chromedriver.exe");
    SmartClientWebDriver driver = new SmartClientChromeDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    Thread.sleep(3000);
    driver.get("xxxxxx"); //here I have given the URL of my web application
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.titleContains("XXXX")); // here I've given the title of my web application
    wait.until(ExpectedConditions.elementToBeClickable(By.id("frontCanvas")));
    Thread.sleep(4000);
    WebElement Draw = driver.findElement(ByScLocator.scLocator("//HLayout[ID=\"long_ribbon_HLayout\"]/member[Class=IconMenuButton||index=1||length=20||classIndex=0||classLength=4||roleIndex=0||roleLength=15||scRole=button]/icon"));
    Draw.click();                                                 
    Thread.sleep(2000);
  }

}

下面给出的是我想要点击的图像+按钮元素的 HTML 代码。

<div id="isc_C" eventproxy="isc_IconButton_Client_0" role="button" aria-label="XXXX" style="position: absolute; left: 80px; top: 0px; width: 42px; height: 42px; z-index: 200090; box-sizing: border-box; overflow: hidden; cursor: pointer;" onscroll="return isc_IconButton_Client_0.$lh()">
   <div id="isc_D" eventproxy="isc_IconButton_Client_0" style="position: relative; display: inline-block; box-sizing: border-box; width: 100%; vertical-align: top; visibility: inherit; z-index: 200090; cursor: pointer;">
     <table role="presentation" width="42" height="42" cellspacing="0" cellpadding="0">
       <tbody>
         <tr>
            <td class="iconButton" style="padding: 4px; background-color: transparent;" valign="top" nowrap="true" align="center">
              <img src="XXXX.png" style="vertical-align:middle;margin-bottom:5px;" eventpart="icon" suppress="TRUE" draggable="true" width="32" height="32" border="0" align="TEXTTOP">
                <br>
            </td>
          </tr>
      </tbody>
    </table>
 </div>
</div>

我应该怎么做才能解决这个问题?

【问题讨论】:

  • 什么是 HTML 布局?将 html 代码发布到您的问题中
  • 对不起,由于我公司的规定,我不能在这里发布 HTML 代码。但是我可以给你一些关于那个布局的相关信息,你可以问。
  • HTML 是否有 svg 标签?
  • 只显示您感兴趣的元素的 HTML。那也是主要标签,里面什么都没有
  • @TarunLalwani,我已经编辑了帖子并为我想要定位的元素添加了 HTML 代码。实际上,我想以按钮的形式单击位于顶部栏上的图像元素,如图所示。

标签: java selenium selenium-webdriver gwt smartgwt


【解决方案1】:

首先你下面的代码是错误的

WebElement Draw = driver.findElement(ByScLocator.scLocator("//HLayout[ID=\"long_ribbon_HLayout\"]/member[Class=IconMenuButton||index=1||length=20||classIndex=0||classLength=4||roleIndex=0||roleLength=15||scRole=button]/icon"));

您需要将ByScLocator.scLocator 替换为xpathcssSelector,如下所示

WebElement Draw = driver.findElement(By.xpath("//HLayout[ID=\"long_ribbon_HLayout\"]/member[Class=IconMenuButton||index=1||length=20||classIndex=0||classLength=4||roleIndex=0||roleLength=15||scRole=button]/icon"));

现在您编写的 Xpath 和您共享的 HTML 完全不同了。

根据你的 HTML,如果你想点击下面的元素

<td class="iconButton" style="padding: 4px; background-color: transparent;" valign="top" nowrap="true" align="center">

然后使用xpath作为

//td[@class='iconButton']

//td[@class='iconButton' and @valign='top']

现在如果你的元素在下面:-

<img src="XXXX.png" style="vertical-align:middle;margin-bottom:5px;" eventpart="icon" suppress="TRUE" draggable="true" width="32" height="32" border="0" align="TEXTTOP">

然后你可以使用:-

(//td[@class='iconButton']/img[@eventpart='icon'])[3]

【讨论】:

  • 我尝试像您一样将 ByScLocator.scLocator 替换为 xpath,但它再次显示相同的错误。除此之外,如果我将按照您的建议使用 XPath "//td[@class='iconButton']/img[@eventpart='icon']" 那么据我所知,它将单击第一个图像图标具有这些属性,但我的图标在列表中排名第三或第四。
  • 根据您所说的位置更新了我的 xpath。目前它被设置为第三名,您可以根据自己的喜好进行更改
  • 您的方法适用于 Chrome、Firefox 和 Edge,但不适用于 Internet Explorer。该怎么办?我在这里发布了这个问题:stackoverflow.com/questions/46094310/…
猜你喜欢
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多