skyhirider · GitHub

I am using an xpath that includes a special character and the xpath is working in Firefox but failing in HtmlUnit when called via the driver.

What is odd is that it works for "@x" but fails for "@x" - case sensitivity breaks it somehow. Its also interesting that I noticed this earlier when searching for "=)" as part of a string, I presume there are other special cases that are not working correctly.

Edit: tested more and same issue with ")X" but lowercase x works fine.

The code I used to test

public class HtmlUnitXpath {
    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "\\geckodriver.exe");
        FirefoxOptions options = new FirefoxOptions();
        options.setLogLevel(FirefoxDriverLogLevel.FATAL);
        WebDriver ffDriver = new FirefoxDriver(options);
        WebDriver huDriver = new HtmlUnitDriver(BrowserVersion.FIREFOX, true) {
            @Override
            protected WebClient modifyWebClient(WebClient client) {
                final WebClient webClient = super.modifyWebClient(client);
                WebClientOptions options = webClient.getOptions();
                options.setCssEnabled(true);
                options.setThrowExceptionOnScriptError(false);
                webClient.setJavaScriptErrorListener(new SilentJavaScriptErrorListener());
                return webClient;
            }
        };
        try {
            doStuff(ffDriver);
            doStuff(huDriver);
        } finally {
            ffDriver.close();
            huDriver.close();
        }
    }
    private static void doStuff(WebDriver driver) {
        String misbehavingString = "@X";
        String pageAsText = "<body><table><tr><td>" + misbehavingString + "</td></tr></table></body>";
        driver.get("data:text/html;charset=utf-8," + pageAsText);
        String text = driver.findElement(By.xpath("//body")).getText();
        System.out.println(driver.getClass().getName() + " printed " + text);
        int numberOfOccurences = driver.findElements(By.xpath("//td[normalize-space()='" + misbehavingString + "']")).size();
        System.out.println(driver.getClass().getName() + " found " + numberOfOccurences);
    }
}

Read the original on github.com ↗