【问题标题】:Reading Unicode Characters using Selenium Webdriver in Java在 Java 中使用 Selenium Webdriver 读取 Unicode 字符
【发布时间】:2021-03-25 22:00:39
【问题描述】:

我正在尝试获取以下 Web 元素的文本,包括其 unicode 字符(版权符号)。

© 2021 ABC Inc. 保留所有权利。 enter image description here

我尝试了 getWebDriver().findElement(elem).getText() 但这给了我以下输出。

? 2021 ABC Inc. 保留所有权利。

我之前看到了一些关于此的帖子,但仍然无法弄清楚如何阅读此 Web 元素,以便我也捕获 unicode 符号 (©)。

感谢您在这方面的任何建议。 谢谢!

【问题讨论】:

  • 非常可能获得正确的文本,但在显示时失败。你如何输出文本?
  • 请包含一小段读取此数据然后显示/打印的代码。
  • 你好 Joachim,Andrew,这里是 webelement/code/output。
  • (WebElement 定义) private static final By copyrightMessage = By.xpath("//span[@class='login-copyright-msg']");
  • (CODE) String copyright = getWebDriver().findElement(copyrightMessage).getText(); System.out.println(String.format("Copyright: %s", copyright));

标签: java selenium-webdriver unicode utf-8


【解决方案1】:

更新:发现十进制数字 169 是 Unicode 和 Windows-1252 中的版权符号字符后,我感到很困惑。所以我不知道到底发生了什么!

如果代码对任何试图解开这个谜团的人有帮助,我将保持原样。


可能是由于有限的(非Unicode)字符集和以任何方式生成输出时使用的编码。

这里是演示代码,显示您的示例字符串通过System.out 转储到控制台,使用当前默认Charset,使用UTF-8,并使用有限的legacy Windows-1252

请参阅此示例code run live at IdeOne.com

import java.util.*;
import java.lang.*;
import java.io.*;

import java.nio.charset.StandardCharsets ;
import java.nio.charset.Charset ;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String blurb = "© 2021 ABC Inc. All rights reserved." ;
        
        // The character set and encoding currently in use by `System.out` is not known, some default.
        System.out.println( "----------|  default  |--------------------------" );
        System.out.println( "blurb: " + blurb ) ;  
        
        // Let's set the character set and encoding to UTF-8 by wrapping `System.out` in a `PrintStream`.
        System.out.println( "----------|  UTF-8  |--------------------------" );
        try
        {
            PrintStream printStream = new PrintStream( System.out , true , StandardCharsets.UTF_8.name() );
            printStream.println( "blurb: " + blurb );
        }
        catch ( UnsupportedEncodingException e )
        {
            e.printStackTrace();
        }
        
        // In contrast, try Windows-1252 character set.
        System.out.println( "----------|  windows-1252  |--------------------------" );

        // Verify windows-1252 charset is available on the current JVM.
        String windows1252CharSetName = "windows-1252";
        boolean isWindows1252CharsetAvailable = Charset.availableCharsets().keySet().contains( windows1252CharSetName );
        if ( isWindows1252CharsetAvailable )
        {
            System.out.println( "isWindows1252CharsetAvailable = " + isWindows1252CharsetAvailable );
        } else
        {
            System.out.println( "FAIL - No charset available for name: " + windows1252CharSetName );
        }

        // Print the blurb.
        try
        {
            PrintStream printStream = new PrintStream( System.out , true , windows1252CharSetName );
            printStream.println( "blurb: " + blurb );
        }
        catch ( UnsupportedEncodingException e )
        {
            e.printStackTrace();
        }
        
    }
}

运行时。

----------|  default  |--------------------------
blurb: © 2021 ABC Inc. All rights reserved.
----------|  UTF-8  |--------------------------
blurb: © 2021 ABC Inc. All rights reserved.
----------|  windows-1252  |--------------------------
isWindows1252CharsetAvailable = true
blurb: � 2021 ABC Inc. All rights reserved.

正如预期的那样,我们看到COPYRIGHT SIGN 字符(十进制代码点 169)在 Unicode 中正确显示,但在 Windows-1252 中失败。 According to Wikipedia,


推荐阅读:The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

【讨论】:

  • 谢谢罗勒!这绝对适合我。我指的是使用 getWebDriver().findElement(By).getText() 方法获取元素值。那只会返回?对于 unicode 字符。想知道是否有其他方法可以使用 selenium webdriver 来获取带有 unicode char 的字符串。
  • @Saurabh 在收到您的评论后,我又做了一些检查。哎呀! Unicode 和 Windows-1252 中的十进制数字 169 恰好是 COPYRIGHT SIGN 字符。所以现在我很困惑。我不知道这里会发生什么。
  • 版权标志的共同出现非常清楚:Windows-1252 是基于 ISO-8859-1 的派生/基于 ISO-8859-1 和 Unicode 标准的创建方式,其代码点是ISO-8859-1(即所有 ISO-8859-1 字符都使用与其 Unicode 代码点相同的值进行编码)。
猜你喜欢
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 2018-11-18
  • 1970-01-01
  • 2013-05-25
  • 2016-06-13
相关资源
最近更新 更多