英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论