繁体   English   中英

如果我将提供第一栏的文字,我该如何获取第二栏的文字

[英]How can i get text of 2nd column if i will provide the text of 1st column

我有一个包含2列的表。 第一列包含金额的类型,即书费,注册费,第二列包含金额的即500和400。我想获取第二列的文本(金额),如何通过提供文本来获取第二列的文本由于动态数据集,仅给出第二列的xpath对我来说不起作用。

数量类型的xpath为:

driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[1]/td[1]")).getText();  (text=Book Fee)
driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[4]/td[1]")).getText(); (text= Registration fees)

金额的xpath为:

driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[1]/td[2]")).getText();  (text= 500.00/=)
driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[4]/td[2]")).getText(); (text=1000.00/=)

尝试以下XPath for Book Fee费用:

//*[@id='charges']/table[2]/tr/td[text()='Book Fee']/../td[2]

尝试以下XPath for Registration fees费用:

//*[@id='charges']/table[2]/tr/td[text()='Registration fees']/../td[2]

您可以使用String concatanation参数化第一列的值以使其适用于所有人:

//*[@id='charges']/table[2]/tr/td[text()=' " + feeType + "']/../td[2]

这里的feeType ,是您作为参数传递给方法的值。

根据您的问题和共享的HTML ,可以通过以下代码提取文本Book Fee and Registration Fees

String bookFeeText = driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[1]/td[1]")).getText();
String registrationFeeText = driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[4]/td[1]")).getText();

现在,要检索下几列的文本,可以使用从以前的列中提取的文本的帮助,如下所示:

driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[1]/td[.='" + bookFeeText + "']//following::td[1]")).getText();
driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[4]/td[.='" + registrationFeeText + "']//following::td[1]")).getText();

因此,基本上,无论费用在表中的位置如何,您都希望根据费用类型获取金额。

List <WebElement> fee_List=driver.findElements(By.xpath("//*[@id='charges']/table[2]/tbody/tr"));
WebElement bookFeeRow=driver.findElement(By.xpath("//td[text()='Book Fee']/.."));
int bookFeeIndex=fee_List.indexOf(bookFeeRow)+1; //'+1' because index starts from 0 and webelement [indices] start from 1
String bookFeeAmount=driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr["+bookFeeIndex+"]/td[2]")).getText(); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM