[英]How can I search a value in a table and then get the other cell value on the same row?
I want to write a generic method, which I can do a search on a table to locate a specific row. 我想编写一个通用方法,可以在表上进行搜索以找到特定的行。 Then, I can get the other values on the same row.
然后,我可以在同一行上获取其他值。
<table>
<tr>
<td>Amazon</td>
<td>$100.00</td>
</tr>
<tr>
<td>Samsung</td>
<td>$200.00</td>
</tr>
</table>
For example, I pass amazon to the method. 例如,我将亚马逊传递给该方法。 The method will locate the row contains Amazon.
该方法将找到包含Amazon的行。 And, it will get the value = $100.00 on the same row.
并且,它将在同一行上获得= $ 100.00的值。
How can i do it using Selenium Java? 我如何使用Selenium Java? Thanks
谢谢
The following method may be help you. 以下方法可能会对您有所帮助。
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();
}
}
You can use below code to get your value from table pass amazon into str then you will get respective value as $100.00 您可以使用以下代码从表格传递到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.