Sahar Millis
Sahar Millis

Reputation: 907

Selenium FirefoxDriver getText - UTF-8 encoding

I searched the web for quit some time, and i can't figure it out.

While using Selenium with the FirefoxDriver, can't perform actions on the text cuz i keep getting ???? instead of actual characters.

Source file example:

enter image description here

Java code:

WebDriver driver = new FirefoxDriver();

I have tried to use some profiles i found online - did not work.

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

I think the problem is in the default encoding of Firefox, witch is "western".
and i need it to be "UTF-8" , or changing according to the site.

All answers to the questions i saw, did not solve the problem.

Upvotes: 0

Views: 6731

Answers (2)

Andrew Regan
Andrew Regan

Reputation: 5113

I'm 99% certain this is simply a matter of the OP not having a Hebrew font installed (e.g.) on their client machine.

After a bit of detective work, this site seems very close to the site being tested. It's not exactly the same, but I think it shows the difference having the right font has.

enter image description here

Of course, not having the right font doesn't stop you validating the server's output.

@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(), "תחנת מידע לסטודנט המכללה האקדמית עמק יזרעאל");
}

is a quick way to verify that the server's output is correct, even if your web-client of choice can't render said output.

Upvotes: 1

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Hi i have done a work around for the question the idea is that

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 have attached a sample piece of working code please have a look

   public static void main(String[] args) throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","D:\\eclipseProject\\StackOverFlow\\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();

    }

Upvotes: 1

Related Questions