Selenium FirefoxDriver getText - UTF-8 编码

Selenium FirefoxDriver getText - UTF-8 encoding

我在网上搜索了一段时间退出,我想不通。

同时使用 SeleniumFirefoxDriver, 无法对文本执行操作,因为我不断收到 ????而不是实际字符。

源文件示例:

Java代码:

WebDriver driver = new FirefoxDriver();

我尝试使用我在网上找到的一些配置文件 - 没有用。

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference( <something>, <something> );
profile.setPreference( <something>, <something> );
driver = new FirefoxDriver( profile );

我认为问题出在 Firefox 的 默认编码 , 是 "western"。
我需要它是 "UTF-8" ,或者根据站点进行更改。

我看到的所有问题的答案,都没有解决问题。

你好,我已经解决了这个问题,我的想法是

1.First take all value inside the list(here i think you are getting ???? instead of actual charterers .)
2.Now save all values inside the text document which save all value in UTF-8 format
3.Now read those values from the text file (now they will print the way they are in web app)
4. Now perform your next operation

5.i 已附上一段工作代码示例,请查看

   public static void main(String[] args) throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","D:\eclipseProject\Whosebug\chromedriver_win32 (1)\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Ftab%3Dwm");

        // below in the right corner there is a Drop-down with values of languages for different country
        // take them all inside the list
        List<WebElement> myddval = driver.findElements(By.xpath("//*[@id='lang-chooser']/option"));
        System.out.println(myddval.size());

        // print the last value inside the dd on to the console
        System.out.println("DD value before Change (ANSI format) : "+ myddval.get(myddval.size()-1).getText()); // will print ???? in the console
        // now create a text document which saves its content form ANSI to UTF-8 format
        writer = new PrintWriter("C:\Users\rajnish\Desktop\name.txt", "UTF-8");
        // writing only the last value form dd in the text document = 繁體中文
        writer.write(myddval.get(myddval.size()-1).getText());
        System.out.println("value in the dd is : " + myddval.get(myddval.size()-1).getText());
        writer.close();
        // reading the value and printing it in the console now it will print =  繁體中文
        bufferedReader = new BufferedReader(new FileReader("C:\Users\rajnish\Desktop\name.txt"));
        System.out.println("Reading Text after converting it to UTF-8 Encoding : "+ bufferedReader.readLine());

        // now performing some action
        driver.findElement(By.id("lang-chooser")).click();
        Thread.sleep(2000);
        myddval.get(myddval.size()-1).click();

    }

我 99% 确定这只是 OP 在其客户端计算机上未安装希伯来语字体 (e.g.) 的问题。

经过一些侦探工作,this site 似乎非常接近正在测试的站点。它不完全相同,但我认为它显示了正确字体的不同之处。

当然,没有正确的字体不会阻止您验证服务器的输出。

@Test
public void testHebrew() {
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://yedion.yvc.ac.il");
    Assert.assertEquals( driver.getTitle(), "תחנת מידע לסטודנט המכללה האקדמית עמק יזרעאל");
}

是验证服务器输出是否正确的快速方法,即使您选择的 Web 客户端无法呈现所述输出。