代码之家  ›  专栏  ›  技术社区  ›  Sahar Millis

Selenium FirefoxDriver getText-UTF-8编码

  •  0
  • Sahar Millis  · 技术社区  · 10 年前

    我在网上搜索了一段时间想辞职,但我想不出来。

    使用时 使用 Firefox驱动程序 , 无法对文本执行操作,因为我一直收到????而不是实际字符。

    源文件示例:

    enter image description here

    Java代码:

    WebDriver driver = new FirefoxDriver();
    

    我尝试过使用我在网上找到的一些个人资料,但没有成功。

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

    我认为问题在于 Firefox的默认编码 ,女巫是“西部”。
    我需要它是“UTF-8” ,或根据站点进行更改。

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

    2 回复  |  直到 10 年前
        1
  •  1
  •   Andrew Regan IAmAliYousefi    10 年前

    我99%肯定这只是因为OP没有安装希伯来文字体( e.g. )在他们的客户机上。

    经过一点侦探工作, this site 看起来非常接近正在测试的站点。不是的 确切地 同样,但我认为它显示了使用正确字体的区别。

    enter image description here

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

    @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客户端无法呈现所述输出。

        2
  •  1
  •   Rajnish Kumar    10 年前

    嗨,我为这个问题做了一个变通,想法是

    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.我附上了一个工作代码示例,请查看

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