JMETER WebDriver - 具有 Groovy 的动态元素

JMETER WebDriver - Dynamic Element with Groovy

我已经在此处复制了 XML 代码。你能告诉我下面示例中的代码吗?非常感谢

WDS.browser.findElement(org.openqa.selenium.By.xpath("//li[contains(@id, 'cascader-menu')]/span[0] ")).click();

如果硬编码为“//li[@id='cascader-menu-9503-0-3']/span”,它将适用于当前网页。但是重装后就不行了

示例代码格式:id="cascader-menu-8497-0-10" id="cascader-menu-(动态)-(0->2)-(1->10)"

<div class="el-cascader-panel"> <div class="el-scrollbar el-cascader-menu" role="menu" id="cascader-menu-8497-0"> <div class="el-cascader-menu__wrap el-scrollbar__wrap" style="margin-bottom: -17px; margin-right: -17px;"> <ul class="el-scrollbar__view el-cascader-menu__list"> <li role="menuitem" id="cascader-menu-8497-0-0" tabindex="-1" class="el-cascader-node in-active-path" aria-haspopup="true" aria-owns="cascader-menu-8497-0" aria-expanded="true"> <span class="el-cascader-node__label">九龍</span><i class="el-icon-arrow-right el-cascader-node__postfix"> </i> </li> <li role="menuitem" id="cascader-menu-8497-0-1" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"><span class="el-cascader-node__label">Australia</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li><li role="menuitem" id="cascader-menu-8497-0-2" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">British Columbia</span> <i class="el-icon-arrow-right el-cascader-node__postfix"> </i> </li> <li role="menuitem" id="cascader-menu-8497-0-3" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">Greece</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li><li role="menuitem" id="cascader-menu-8497-0-4" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">Hong Kong Island</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li><li role="menuitem" id="cascader-menu-8497-0-5" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">Kowloon</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li><li role="menuitem" id="cascader-menu-8497-0-6" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">New Territories</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li> <li role="menuitem" id="cascader-menu-8497-0-7" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">Singapore</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li> <li role="menuitem" id="cascader-menu-8497-0-8" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">Taiwan</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li><li role="menuitem" id="cascader-menu-8497-0-9" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">UK</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li> <li role="menuitem" id="cascader-menu-8497-0-10" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-8497-0"> <span class="el-cascader-node__label">United States</span> <i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li><svg class="el-cascader-menu__hover-zone"> <path style="pointer-events: auto;" fill="transparent" d="M-152 6 L183 0 V6 Z"></path> <path style="pointer-events: auto;" fill="transparent" d="M-152 40 L183 187 V40 Z"></path> </svg></ul></div><div class="el-scrollbar__bar is-horizontal"><div class="el-scrollbar__thumb" style="transform: translateX(0%);"> </div> </div> <div class="el-scrollbar__bar is-vertical"> <div class="el-scrollbar__thumb" style="transform: translateY(0%); height: 48.4456%;"> </div></div></div><div class="el-scrollbar el-cascader-menu" role="menu" id="cascader-menu-4173-1"> <div class="el-cascader-menu__wrap el-scrollbar__wrap" style="margin-bottom: -17px; margin-right: -17px;"> <ul class="el-scrollbar__view el-cascader-menu__list"> <li role="menuitem" id="cascader-menu-4173-1-0" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu-4173-1"> <span class="el-cascader-node__label">九龍東</span><i class="el-icon-arrow-right el-cascader-node__postfix"></i> </li><svg class="el-cascader-menu__hover-zone"> <path style="pointer-events: auto;" fill="transparent" d="M178.40625 6 L207 0 V6 Z"></path> <path style="pointer-events: auto;" fill="transparent" d="M178.40625 40 L207 187 V40 Z"></path> </svg> </ul> </div> <div class="el-scrollbar__bar is-horizontal"> <div class="el-scrollbar__thumb" style="transform: translateX(0%);"> </div> </div> <div class="el-scrollbar__bar is-vertical"> <div class="el-scrollbar__thumb" style="transform: translateY(0%);"> </div> </div> </div> </div>

  1. 不是XML,是HTML
  2. 它不包含您要查找的元素

无论如何,您可以坚持使用位置文本,例如:

//span[text()='Shaukeiwan`]

在此之前,您可能需要单击此 Hong Kong Island,然后单击 Mid Level North Point,并且不要忘记使用 Explicit Wait 以确保该元素是 available/visible/can点击

您可以查看 The WebDriver Sampler: Your Top 10 Questions Answered 文章中的示例实现。