PatternSyntaxException while String split_Module 13_execise | Selenium Forum
M
Posted on 29/01/2017
I am trying to solve second question in Exercise of Module 13

[b:1li2jean]"Go to:
http://www.americangolf.co.uk/golf-clubs/fairway-woods"
Print every link under brand
Every link will have a number in front of it
Find sum of all numbers
Total number of products found is written on the page-"74 Products found"
Verify sum s equal to number of products mentioned[/b:1li2jean]

I am able to print links, but error while splitting string to get numbers.

Kindly refer attachment to check issue and suggest if any other solution to this problem as stuck up with this for long tim. (provide complete solution is possible)

M
Replied on 29/01/2017

Here is solution:

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.xerces.impl.xpath.regex.Match;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class exerc3 {

static WebDriver d = null;

public static void main(String[] args) {

d = new FirefoxDriver();
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
d.navigate().to("http://www.americangolf.co.uk/golf-clubs/fairway-woods");

WebElement brand=d.findElement(By.xpath("//*[@id='secondary']/div[1]/div[3]/div"));
List<WebElement>brandlist=brand.findElements(By.tagName("a"));
System.out.println(brandlist.size());

for(int i=0;i<brandlist.size();i++){

System.out.println(i+")"+brandlist.get(i).getText());
}


String parta ="//*[@id='secondary']/div[1]/div[3]/div/ul/li[";
String partb="]/a/span[2]";
int k=1;
int totalvalue=0;

while(brandnumber(parta+k+partb)){

String number=d.findElement(By.xpath(parta+k+partb)).getText();
System.out.println(number);

String n = number.replaceAll( "[^\\d\\-]", "" ); // This line of code is used to replace all special characters in java
System.out.println(n);

int sum=Integer.parseInt(n);

totalvalue=totalvalue+sum;
k++;
}
System.out.println("Total sum of all brands is"+"---"+totalvalue);

String productcount=d.findElement(By.xpath("//*[@id='primary']/div[2]/div[1]/div[1]/span")).getText();

int productstotal=Integer.parseInt(productcount);

if(totalvalue==productstotal){

System.out.println(true);

} else {
System.out.println(false);
}

}

public static boolean brandnumber(String abc){

List<WebElement>a=d.findElements(By.xpath(abc));

if(a.size()==0)

return false;
else
return true;

}

}