I get color of the text and stored in ‘textColor’. Now i need to pass this color to font.setColor. I mean instead of hard code color, i need to pass the textColor to font.setColor which mean i need to place it instead of Light_ORANGE. Could anyone help me to fix this. At the end i need to extract the text with color and the same text with color need to be write in excel.
WebElement winner = driver.findElement(By.xpath(“//div[@url=’/api/html/cricket-scorecard/23253′]/div[1]”));
JavaScript
x
String textColor = winner.getCssValue("color");
System.out.println(textColor);
Pattern c = Pattern.compile("rgba *\(*([0-9]+), *([0-9]+), *([0-9]+), *([0-9]+) *\)");
Matcher m = c.matcher(textColor);
m.matches();
Color awtColor = new Color(Integer.valueOf(m.group(1)), Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)), Integer.valueOf(m.group(4)));
File file = new File("D:\SELVA\24GB\Cucumber-Project\scorecard.xlsx");
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sh = wb.createSheet("Scorecard");
XSSFCell cell = sh.createRow(0).createCell(0);
cell.setCellValue(winner.getText());
XSSFFont xssfFont = wb.createFont();
XSSFColor xssfColor = new XSSFColor(awtColor);
xssfFont.setColor(xssfColor);
FileOutputStream fos = new FileOutputStream(file);
wb.write(fos);
wb.close();
Advertisement
Answer
String textColor = winner.getCssValue("color");
returns a String, such as rgba(0, 0, 0, 1)
.
You can modify your code refer to this example:
JavaScript
import java.awt.Color;
Pattern c = Pattern.compile("rgba *\( *([0-9]+), *([0-9]+), *([0-9]+), *([0-9]+) *\)");
Matcher m = c.matcher(textColor);
/*
* The code will be more robust if add validation of `textColor` content
*/
m.matches();
Color awtColor = new Color(Integer.valueOf(m.group(1)), // r
Integer.valueOf(m.group(2)), // g
Integer.valueOf(m.group(3)), // b
Integer.valueOf(m.group(4))); // a
XSSFColor xssfColor = new XSSFColor(awtColor);
xssfFont.setColor(xssfColor);