如何使用 Katalon Studio 通过 Selenium 在子 WebElements 列表中找到具有 'checked' 属性的 Webelement
How to find a webelement that has 'checked' attribute in list of child WebElements through Selenium using Katalon Studio
我有单选按钮,当任一单选按钮被选中时,它会获得一个 checked
属性。
这是 HTML 的样子:
我获取具有 checked
属性的后代的实现:
public TestObject getCheckedTestObjectFromParent(String parentID){
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
List<WebElement> children = parentWebElement.findElements(By.xpath(".//*"))
println(children.size())
for(int i = 0; i < children.size(); i++){
TestObject childTestObject = getTestObjectFromWebElement(children[i])
if(WebUI.verifyElementHasAttribute(childTestObject, 'checked', 10, FailureHandling.OPTIONAL)){
return childTestObject
}
}
}
这是我用来将 WebElement
转换为 TestObject
的辅助方法:
public TestObject getTestObjectFromWebElement(WebElement element) {
TestObject object = new TestObject()
object.addProperty("xpath", ConditionType.CONTAINS, getXPathFromElement(element))
return object
}
帮助从 WebElement
获取 xpath
:
protected String getXPathFromElement(WebElement element) {
String elementDescription = element.toString();
return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));
}
我是不是遗漏了什么或者 WebElement
-> TestObject
转换有问题?这也可能仅使用 TestObject 或仅使用 WebElement 吗?如果我可以让子 TestObjects
包含来自父 TestObject
的某些属性,那么我就不需要使用 WebElements
搞得一团糟了。
编辑
HTML 的另一张图片,这次勾选了第一个单选按钮。如您所见,第二个单选按钮不再具有 'checked' 属性。
试试这个 Xpath
"//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"
示例:
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"))
应该return尺码1
编辑
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio']"));
for (int i=0;i<children.size();i++)
{
if(children.get(i).getAttribute("checked")!=null)
{
System.out.println("radio button is checked");
}
}
我可以通过将 (".//*")
更改为 (".//*[@checked='checked']")
来解决这个问题
parentWebElement.findElement(By.xpath(".//*[@checked='checked']")
将找到具有属性 checked = 'checked'
的元素
请注意,不再需要列表,因为一次只能选中 1 个单选按钮。
实施
public TestObject getCheckedTestObjectFromParent(String parentID){
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
//there is only 1 checked child at a time, so there is no need for a list
WebElement checkedChild = parentWebElement.findElement(By.xpath(".//*[@checked='checked']"))
//convert the WebElement to a TestObject and return
return getTestObjectFromWebElement(checkedChild)
}
要检索当前 checked
的 WebElement,您可以使用以下任一方法 :
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.a-toggle.a-toggle--anycase#config-src-laserunits div[id^='config-src-laserunits-']>input.a-toggle__radio[checked]")));
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='a-toggle a-toggle--anycase' and @id='config-src-laserunits']//div[starts-with(@id, 'config-src-laserunits-')]/input[@class='a-toggle__radio' and @checked]")));
我有单选按钮,当任一单选按钮被选中时,它会获得一个 checked
属性。
这是 HTML 的样子:
我获取具有 checked
属性的后代的实现:
public TestObject getCheckedTestObjectFromParent(String parentID){
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
List<WebElement> children = parentWebElement.findElements(By.xpath(".//*"))
println(children.size())
for(int i = 0; i < children.size(); i++){
TestObject childTestObject = getTestObjectFromWebElement(children[i])
if(WebUI.verifyElementHasAttribute(childTestObject, 'checked', 10, FailureHandling.OPTIONAL)){
return childTestObject
}
}
}
这是我用来将 WebElement
转换为 TestObject
的辅助方法:
public TestObject getTestObjectFromWebElement(WebElement element) {
TestObject object = new TestObject()
object.addProperty("xpath", ConditionType.CONTAINS, getXPathFromElement(element))
return object
}
帮助从 WebElement
获取 xpath
:
protected String getXPathFromElement(WebElement element) {
String elementDescription = element.toString();
return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));
}
我是不是遗漏了什么或者 WebElement
-> TestObject
转换有问题?这也可能仅使用 TestObject 或仅使用 WebElement 吗?如果我可以让子 TestObjects
包含来自父 TestObject
的某些属性,那么我就不需要使用 WebElements
搞得一团糟了。
编辑
HTML 的另一张图片,这次勾选了第一个单选按钮。如您所见,第二个单选按钮不再具有 'checked' 属性。
试试这个 Xpath
"//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"
示例:
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"))
应该return尺码1
编辑
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio']"));
for (int i=0;i<children.size();i++)
{
if(children.get(i).getAttribute("checked")!=null)
{
System.out.println("radio button is checked");
}
}
我可以通过将 (".//*")
更改为 (".//*[@checked='checked']")
parentWebElement.findElement(By.xpath(".//*[@checked='checked']")
将找到具有属性 checked = 'checked'
请注意,不再需要列表,因为一次只能选中 1 个单选按钮。
实施
public TestObject getCheckedTestObjectFromParent(String parentID){
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
//there is only 1 checked child at a time, so there is no need for a list
WebElement checkedChild = parentWebElement.findElement(By.xpath(".//*[@checked='checked']"))
//convert the WebElement to a TestObject and return
return getTestObjectFromWebElement(checkedChild)
}
要检索当前 checked
的 WebElement,您可以使用以下任一方法
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.a-toggle.a-toggle--anycase#config-src-laserunits div[id^='config-src-laserunits-']>input.a-toggle__radio[checked]")));
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='a-toggle a-toggle--anycase' and @id='config-src-laserunits']//div[starts-with(@id, 'config-src-laserunits-')]/input[@class='a-toggle__radio' and @checked]")));