[英]How can I search a value in a table and then get the other cell value on the same row?
我想編寫一個通用方法,可以在表上進行搜索以找到特定的行。 然后,我可以在同一行上獲取其他值。
<table>
<tr>
<td>Amazon</td>
<td>$100.00</td>
</tr>
<tr>
<td>Samsung</td>
<td>$200.00</td>
</tr>
</table>
例如,我將亞馬遜傳遞給該方法。 該方法將找到包含Amazon的行。 並且,它將在同一行上獲得= $ 100.00的值。
我如何使用Selenium Java? 謝謝
以下方法可能會對您有所幫助。
public String find(WebDriver driver, String searchString){
List<WebElemet> lstElements=driver.findElement(By.xpath("//table/tr/td");
for(int i=0;i<lstElements.size();i++){
if(lstElements.get(i).getText().equals(searchString)){
return lstElments(i+1).getText();
}
}
您可以使用以下代碼從表格傳遞到str的值中獲取您的價值,然后您將分別獲得$ 100.00的價值。
public String getValue(String str){
String xpath = "//td[text()='%s']";
String ele= String.format(xpath,str);
WebElement productname= driver.findElement(By.xpath(ele));
return productname.findElement(By.xpath("/following-sibling::td")).getText();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.