英文:
How to make parameter in xpath and make codes easier to read if there are several same lines, java, selenium
问题
我编写了这个布尔方法,但我想让它更短更智能,因为有3行相同的XPath
。有人可以帮我吗?谢谢。
public boolean VerifyKORSecDispaly() {
boolean a = driver
.findElement(By
.xpath("(//tr[@data-testid='row']//td[@class='kor'])[1]//span[@class='da']"))
.getText().contains("d");
boolean b = driver
.findElement(By
.xpath("(//tr[@data-testid='row']//td[@class='kor'])[2]//span[@class='da']"))
.getText().contains("d");
boolean c = driver
.findElement(By
.xpath("(//tr[@data-testid='row']//td[@class='kor'])[3]//span[@class='da']"))
.getText().contains("d");
return a && b && c;
}
希望这可以帮助您缩短代码并使其更智能。
英文:
I wrote this boolean method but I want to make it shorter and smarter since there are 3 same lines with quite the same XPath
.
Could anyone help me with this, please? thanks
public boolean VerifyKORSecDispaly() {
boolean a = driver
.findElement(By
.xpath("(//tr[@data-testid='row']//td[@class='kor'])[1]//span[@class='da']"))
.getText().contains("d");
boolean b = driver
.findElement(By
.xpath("(//tr[@data-testid='row']//td[@class='kor'])[2]//span[@class='da']"))
.getText().contains("d");
boolean c = driver
.findElement(By
.xpath("(//tr[@data-testid='row']//td[@class='kor'])[3]//span[@class='da']"))
.getText().contains("d");
if (a == true && b == true && c == true) {
return true;
} else {
return false;
}
}
答案1
得分: 1
你可以使用List<>
,因为你在xpath中使用了索引。
//tr[@data-testid='row']//td[@class='kor']
<- 这个选择器会返回多个元素。
基于这些元素,我们可以找到span[@class='da']
元素。
代码:
public boolean VerifyKORSecDispaly() {
boolean a = doesRowTextContain(0, "d");
boolean b = doesRowTextContain(1, "d");
boolean c = doesRowTextContain(2, "d");
if (a == true && b == true && c == true) {
return true;
} else {
return false;
}
}
private boolean doesRowTextContain(int index, String expectedString) {
By spanSelector = By.xpath(".//span[@class='da']"); //点.减小了元素的范围。不再搜索所有元素,而是缩小到父元素的范围
List<WebElement> dataRows = driver.findElements(By.xpath("//tr[@data-testid='row']//td[@class='kor']"));
return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
}
还有一件事是 - 你不需要将a, b或c
与true
进行比较,因为在if
语句中这是默认的预期值。
if (a && b && c) {
return true;
} else {
return false;
}
甚至可以简化为
return a && b && c
最终的方法可能如下所示:
public boolean VerifyKORSecDispaly() {
return doesRowTextContain(0, "d") && doesRowTextContain(1, "d") && doesRowTextContain(2, "d");
}
private boolean doesRowTextContain(int index, String expectedString) {
By spanSelector = By.xpath(".//span[@class='da']"); //点.减小了元素的范围。不再搜索所有元素,而是缩小到父元素的范围
List<WebElement> dataRows = driver.findElements(By.xpath("//tr[@data-testid='row']//td[@class='kor']"));
return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
}
英文:
You could use List<>
since you use indexes in xpath.
//tr[@data-testid='row']//td[@class='kor']
<- this selector would return multiple elements
Based on these elements, we can find span[@class='da']
element.
Code:
public boolean VerifyKORSecDispaly() {
boolean a = doesRowTextContain(0, "d");
boolean b = doesRowTextContain(1, "d");
boolean c = doesRowTextContain(2, "d");
if (a == true && b == true && c == true) {
return true;
} else {
return false;
}
}
private boolean doesRowTextContain(int index, String expectedString) {
By spanSelector = By.xpath(".//span[@class='da']"); //the dot . reduces the scope of the element. Instead of searching through all the elements in source, we'll reduce the scope to parent element
List<WebElement> dataRows = driver.findElements(By.xpath("//tr[@data-testid='row']//td[@class='kor']"));
return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
}
One more thing is - you don't have to compare a, b or c
with true
as it's default expected value in if
statement.
if (a && b && c) {
return true;
} else {
return false;
}
Or even
return a && b && c
Final methods could look like this:
public boolean VerifyKORSecDispaly() {
return doesRowTextContain(0, "d") && doesRowTextContain(1, "d") && doesRowTextContain(2, "d");
}
private boolean doesRowTextContain(int index, String expectedString) {
By spanSelector = By.xpath(".//span[@class='da']"); //the dot . reduces the scope of the element. Instead of searching through all the elements in source, we'll reduce the scope to parent element
List<WebElement> dataRows = driver.findElements(By.xpath("//tr[@data-testid='row']//td[@class='kor']"));
return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论