您有一些选项可以做到这一点,我强烈建议您始终使用 xpath 来访问您想要使用的所有元素。原因是对象报告通常会失败,而且在我看来,这种方式要复杂得多。
但显然,如果 web 发生变化,xpath 会发生变化,所以要小心。
The imports you need:
import static org.junit.Assert.*
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
def driver = DriverFactory.getWebDriver()
//If you want to click your input would be:
WebUI.click(WebUI.convertWebElementToTestObject(driver.findElement(By.xpath("(//input[@id='a-select-paCricteriaId_6908'])"))))
//**you just can click on "TestObject" type, and findElement returns "Element" type**
如果你想选择你需要知道整个路径的选项(我无法用给定的信息得到它)。
测试 xpath 的一个重要提示是在控制台模式 (F12) 下使用此功能:
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
//And this code in the same console to test your xpath:
getElementByXpath("YOURTESTXPATH")
此外,还有其他方法可以使用 xpath 达到相同的目标,例如:
import com.kms.katalon.core.testobject.TestObject as TestObject
...
TestObject tobj = new TestObject('myTestObject')
String expr = '/some/valid/xpath/expression'
tobj.addProperty('xpath', ConditionType.EQUALS, expr)
WebUI.click(tobj)
如果您在 Google 上搜索“如何通过 xpath katalon 获取元素”,您将获得大量信息。
您可以在此处获取有关它的官方信息:
https://docs.katalon.com/katalon-studio/tutorials/detect_elements_xpath.html#what-is-xpath