【问题标题】:How to click on elements in ExtJS using Selenium?如何使用 Selenium 在 ExtJS 中单击元素?
【发布时间】:2013-04-12 19:35:52
【问题描述】:

我的页面上有两个元素(两个“取消”元素)。

<div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen1179">
Cancel
</div>

<div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen2951">
Cancel
</div>

如何点击第二个元素?显然,我不能使用 id,因为它是在每次访问时随机生成的。我可以使用什么?

【问题讨论】:

    标签: extjs selenium webdriver selenium-webdriver


    【解决方案1】:


    1. 使用 FindElements 方法,该方法使用给定机制查找当前上下文中的所有 IWebElement。 (在这种情况下,您始终需要知道要查找的元素的索引。)

    IWebDriver driver = new FirefoxDriver();
    IList<IWebElement> cancelDivs = driver.FindElements(By.XPath("//div[text()='Cancel']"));
    cancelDivs[1].click(); //zero-base index
    


    2.如果取消按钮在不同的部分,可以通过非ExtJS的id属性来识别。

    <div id='header'>
        <div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen1179">Cancel</div>
    </div>
    <div id='footer'>
        <div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen2951">Cancel</div>
    </div>
    


    IWebElement secondCancelDiv = driver.FindElement(By.XPath("//div[@id='footer']//div[text()='Cancel']"));
    secondCancelDiv.Click();
    


    3.如果这些取消按钮在不同的部分,可以通过不同的ExtJS类属性来识别。 (使用有意义的)

    <div id='ext-gen1060' class='x-grid3-body'>
        <div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen1179">Cancel</div>
    </div>
    <div id='ext-gen2555' class='x-toolbar-right-row'>
        <div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen2951">Cancel</div>
    </div>
    


    IWebElement secondCancelDiv = driver.FindElement(By.XPath("//div[@class='x-toolbar-right-row']//div[text()='Cancel']"));
    secondCancelDiv.Click();
    

    【讨论】:

      【解决方案2】:

      如果:

      1. 页面上始终只有 2 个“取消”按钮,并且
      2. 你总是需要第二个,

      使用//div[text()="Cancel"][2] xpath 选择器,或者同时找到它们并单击第二个。

      【讨论】:

      • 更准确地说,xpath=(//div[text()="Cancel"])[2]。即使这两个 div 元素不是同级元素,它也会起作用。
      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 2022-12-04
      • 2019-10-09
      • 2021-12-09
      • 2020-03-08
      • 2017-11-23
      • 1970-01-01
      相关资源
      最近更新 更多