System.setProperty("webdriver.chrome.driver", "C:\\Users\\Testing\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("https://jpetstore.cfapps.io/catalog");
driver.findElement(By.xpath("//a[contains(text(),'Sign In')]")).click();
driver.findElement(By.name("username")).sendKeys("Testing6738788");
driver.findElement(By.name("password")).sendKeys("test@123");
driver.findElement(By.id("login")).click();
driver.findElement(By.xpath("//div[@id='SidebarContent']/a[contains(@href,'FISH')]/img")).click();
driver.findElement(By.xpath("//td[contains(text(),'Angelfish')]//preceding-sibling::td//a")).click();
List<WebElement> tablelist = driver.findElements(By.xpath("//div[@id='Catalog']//tr"));
for(int i = 0; i < tablelist.size(); i++)
{
String gotvalues = tablelist.get(i).getText();
System.out.println("Values got from the table " +gotvalues);
// Here im using split function but no luck
String[] splitword = gotvalues.split(" ");
for(String words : splitword)
{
System.out.println("Got single words from the split " + words);
// I want to compare the Large Angelfish value from the output
if(words.equalsIgnoreCase("Large Angelfish"))
{
System.out.println("Element present " + words);
}
}
}
单词应拆分为“商品ID” -EST-1。我遇到说明问题。完整的单词不会显示。如何编写代码以获取商品ID,产品ID和说明?
答案 0 :(得分:0)
也可以不使用String Array进行验证。请尝试此代码以查看是否有帮助。从键盘上进行输入。您必须在控制台中输入两个值,例如EST-1
,然后按Enter键再输入Large Angelfish
并对其进行比较稍后。立即尝试。
Scanner scan= new Scanner(System.in);
String textID= scan.nextLine(); //Enter ID Here
String textDesc= scan.nextLine();//Enter Desc Here
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Testing\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("https://jpetstore.cfapps.io/catalog");
driver.findElement(By.xpath("//a[contains(text(),'Sign In')]")).click();
driver.findElement(By.name("username")).sendKeys("Testing6738788");
driver.findElement(By.name("password")).sendKeys("test@123");
driver.findElement(By.id("login")).click();
driver.findElement(By.xpath("//div[@id='SidebarContent']/a[contains(@href,'FISH')]/img")).click();
driver.findElement(By.xpath("//td[contains(text(),'Angelfish')]//preceding-sibling::td//a")).click();
List<WebElement> tablelist = driver.findElements(By.xpath("//div[@id='Catalog']//tr/td"));
System.out.println(tablelist.size());
for(int i=0;i<tablelist.size();i++)
{
String gotvalues = tablelist.get(0).getText();
String gotvaluesdesc = tablelist.get(2).getText();
// System.out.println("Values got from the table " +gotvalues );
if(gotvalues.trim().equalsIgnoreCase(textID) && gotvaluesdesc.trim().equalsIgnoreCase(textDesc))
{
System.out.println("Element present ID: " + gotvalues + " Desc :" + gotvaluesdesc);
break;
}
答案 1 :(得分:0)
要比较表数据时,需要对xPath进行少量修改以有效地获取数据,这样就可以避免拆分部分,并且可以轻松地比较数据。
尝试以下代码:
String xPath = "//div[@id='Catalog']//tr";
List<WebElement> tableList = driver.findElements(By.xpath(xPath));
System.out.println("Item ID\t\tProduct ID\t\tDescription\t\tList Price\t\tOther");
System.out.println("--------------------------------------------------------------------------------------------------");
for(int i=1;i<tableList.size();i++) {
// Below line fetches/stores each table data as column wise
List<WebElement> listData = driver.findElements(By.xpath(xPath+"["+(i+1)+"]/td"));
for(int j=0;j<listData.size();j++) {
// Printing the column data
System.out.print(listData.get(j).getText()+"\t\t");
}
System.out.println();
}
// As per the above output, description is in the 3rd(2nd index) column so you can fetch that with the index number 2.
for(int i=1;i<tableList.size();i++) {
List<WebElement> listData = driver.findElements(By.xpath(xPath+"["+(i+1)+"]/td"));
if(listData.get(2).getText().trim().equals("Large Angelfish")) {
System.out.println("=> 'Large Angelfish' is Matching...");
}
if(listData.get(2).getText().trim().equals("Large Angelfish")) {
System.out.println("=> 'Small Angelfish' is Matching...");
}
}
如果执行上述代码,它将显示以下输出:
Item ID Product ID Description List Price Other
----------------------------------------------------------------------------
EST-1 FI-SW-01 Large Angelfish $16.50 Add to Cart
EST-2 FI-SW-01 Small Angelfish $16.50 Add to Cart
在上面的输出中,“描述”列号为3,因此您可以将以下行中的索引号替换为对应的列:
listData.get(2).getText().trim().equals("Large Angelfish")
希望对您有帮助...