繁体   English   中英

我想从网络表中获取带有颜色的文本并使用 selenium web 驱动程序和 apache poi 在 excel 中打印

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

我获取文本的颜色并存储在“textColor”中。 现在我需要将此颜色传递给 font.setColor。 我的意思是,不是硬编码颜色,我需要将 textColor 传递给 font.setColor,这意味着我需要放置它而不是 Light_ORANGE。 谁能帮我解决这个问题。 最后我需要提取带有颜色的文本,并且需要在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\\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();

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

你可以参考这个例子修改你的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM