如何在 Selenium 中使用 EXTJS 生成的 ext-gen ID?

发布于 2024-11-07 18:47:10 字数 116 浏览 0 评论 0原文

在测试自动化 Web 应用程序时,我获得了动态生成的 ext-gen ID。我尝试使用 xpath 但测试用例失败。我浏览了不同的网站,但没有找到任何运气。有人可以帮助我吗?

谢谢你, 斯里尼瓦斯·马蒂

While test automating a web application, I get dynamically generated ext-gen IDs. I tried using xpath but the test cases are failing. I went through different websites but did not find any luck. Can somebody help me?

Thank you,
Srinivas Marthi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九命猫 2024-11-14 18:47:10

对于自动化测试,最好完全避免使用 ExtJS 自动生成的 ID。您可以将自己的静态 ID 分配给组件,但现在您基本上是在代码中乱扔全局变量,这也不好。使用一些 ID 可能是一个有用的折衷方案,但您不想为每个小按钮分配一个 ID。

对于 ExtJS 4,我建议使用 ComponentQuery :

Ext.ComponentQuery.query("panel[title='Orders'] button[text='Save']")

For automated testing, the auto-generated ID-s of ExtJS are best avoided altogether. You can assign your own static ID-s to components, but now you are basically littering your code with global variables, not good either. Using some ID-s might be a helpful compromise, but you don't want to assign an ID to every single little button.

For ExtJS 4 I suggest using the ComponentQuery:

Ext.ComponentQuery.query("panel[title='Orders'] button[text='Save']")
雨巷深深 2024-11-14 18:47:10

我已经成功地自动化了 EXTJS 站点和自动生成的 id,尽管我不推荐这样做。 (因为 id 是自动生成的,如果将新元素添加到页面,则所有定位器都可能无效。)

我建议精确定位精确的项目,而不是完整路径

//*[@id="ext-js123"]

I've been successful with automating EXTJS sites and auto-generated ids, though I do not recommend it. (because the ids are autogenerated, if new elements are added to the page, all your locators are potentially invalid.)

I'd recommend pin-pointing the precise item, instead of a full path

//*[@id="ext-js123"]
淡水深流 2024-11-14 18:47:10

使用 Selenium 的最好方法是在代码中设置唯一的 ID。

由于没有配置 ButtonId,因此您必须在创建按钮后附加按钮的 ID。
在 ExtJS 3 中,我们曾经设置按钮的 ID:

dlg.getDialog().getEl().select('button').each(function(el) {
    el.dom.id = 'confirm-' + el.dom.innerHTML;
});

不幸的是,这在 ExtJS 4 中不再起作用,所以我也在寻找新的解决方案。 ;-)

The best thing using Selenium is to set unique IDs in the Code.

As there is no config buttonId you have to attach the ID for buttons after creation of the button.
In ExtJS 3 we used to set the ID for buttons:

dlg.getDialog().getEl().select('button').each(function(el) {
    el.dom.id = 'confirm-' + el.dom.innerHTML;
});

Unfortunatly this does not work anymore in ExtJS 4, so I am looking for a new solution also. ;-)

残月升风 2024-11-14 18:47:10

由于id属性的值发生变化,即本质上是动态的,您将无法使用id属性的完整值,而只能使用静态的部分值。作为示例,对于以下 HTML:

<table id='ext-gen1076' class='bats-table bats-table--center'>
[...]
</table>

要识别 <table>您需要为 visibility_of_element_ located() 引入 WebDriverWait 节点,并且可以使用以下任一 定位器策略

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.visibility_of_element_ located((By.CSS_SELECTOR, "table[id^='ext-gen']")))
    
  • 使用 XPATH

    WebDriverWait(driver, 20).until(EC.visibility_of_element_ located((By.XPATH, "//table[starts-with(@id,'ext-gen')]")))
    
  • 注意:您必须添加以下导入:

    从 selenium.webdriver.support.ui 导入 WebDriverWait
    从 selenium.webdriver.common.by 导入
    从 selenium.webdriver.support 导入预期条件作为 EC
    

但是,会有很多其他元素具有以 < 开头的 id 属性代码>ext-gen。因此,要唯一标识 元素,您需要按如下方式组合 class 属性:

  • 使用 CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.visibility_of_element_ located((By.CSS_SELECTOR, "table.bats-table.bats-table--center[id^='ext-gen']")) )
    
  • 使用 XPATH

    WebDriverWait(driver, 20).until(EC.visibility_of_element_ located((By.XPATH, "//table[@class='bats-table bats-table--center' andstarts-with(@id ,'ext-gen')]")))
    

参考

您可以在以下位置找到相关的详细讨论:

As the value of the id attribute changes i.e. dynamic in nature you won't be able to use the complete value of the id attribute and can use only partial value which is static. As an example, for the following HTML:

<table id='ext-gen1076' class='bats-table bats-table--center'>
[...]
</table>

To identify the <table> node you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table[id^='ext-gen']")))
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[starts-with(@id,'ext-gen')]")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

However, there would be a lot other elements with id attribute starting with ext-gen. So to uniquely identify the <table> element you need to club up the class attribute as follows:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table.bats-table.bats-table--center[id^='ext-gen']")))
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@class='bats-table bats-table--center' and starts-with(@id,'ext-gen')]")))
    

Reference

You can find a relevant detailed discussion in:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文