I would like to fetch text with color from web table and print in excel using selenium web driver and apache poi

huangapple go评论140阅读模式
英文:

I would like to fetch text with color from web table and print in excel using selenium web driver and apache poi

问题

WebElement winner = driver.findElement(By.xpath("//div[@url='/api/html/cricket-scorecard/23253']/div[1]"));

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();

英文:

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]"));

	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\GB\\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();

答案1

得分: 1

String textColor = winner.getCssValue("color");
返回一个字符串例如`rgba(0, 0, 0, 1)`。

您可以根据此示例修改您的代码
```java
import java.awt.Color;

Pattern c = Pattern.compile("rgba *\\( *([0-9]+), *([0-9]+), *([0-9]+), *([0-9]+) *\\)");
Matcher m = c.matcher(textColor);
/*
 * 如果添加对`textColor`内容的验证,代码将更加健壮
 */
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);
英文:

String textColor = winner.getCssValue("color"); returns a String, such as rgba(0, 0, 0, 1).

You can modify your code refer to this example:

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);

huangapple
  • 本文由 发表于 2020年9月7日 02:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63767753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定