question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

normalize space not parsing correctly under some circumstances

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rbricommented, Sep 24, 2021

nice catch - found the reason and start thinking about a solution

0reactions
skyhiridercommented, Sep 25, 2021

Yeah, I always try to imagine if it was me doing the bug testing and fixing, the more info I have the faster I can fix it. Plus shows I at least tried to find the cause instead of pasting a generic error code and asking for help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

normalize-space just works with xpath not css selector
Unfortunately, XPath functions are not available with CSS selectors in Scrapy. You could first translate your div[class=location]::text CSS ...
Read more >
How normalize-space Function Work in XSLT? - eduCBA
This is a guide to XSLT normalize-space. Here we discuss the introduction, how normalize-space function work in XSLT? and examples.
Read more >
normalize-space - XPath - MDN Web Docs
The normalize-space function strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single ...
Read more >
Advanced Xpath Concept - Method normalize-space & Its Usage
Hello Folks,. In this post we will going to learn an advanced concept of xpath: – normalize-space method. Before we discuss about method ......
Read more >
XML Normalization - W3C
The normalized form of an XML document may not be completely operational within the application context, though the circumstances under ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found