Skip to content

Instantly share code, notes, and snippets.

@Robdel12
Last active February 15, 2019 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robdel12/82061f97674a9aa42fdd59e92e657760 to your computer and use it in GitHub Desktop.
Save Robdel12/82061f97674a9aa42fdd59e92e657760 to your computer and use it in GitHub Desktop.
The smallest needed reproduction for UTF-8 / CSS child selector encoding issues with Chrome webdriver

What is this?

When using the Chrome webdriver with inline styles that have a child selector (>), calling browser.page_source improperly encodes the child selector to >.

This works properly in Firefox and Safari, but not Chrome.

This example was served with a SimpleHTTPServer in Python (python -m SimpleHTTPServer). I even went as far as to try and serve these files with UTF-8 encoding:

python -c "import SimpleHTTPServer; m = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map; m[''] = 'text/plain'; m.update(dict([(k, v + ';charset=UTF-8') for k, v in m.items()])); SimpleHTTPServer.test();"

Given the example files in this gist, I'd expect browser.page_source to return:

Expected

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
    <title>Example page</title>
    <meta charset="UTF-8" />

    <style>
     div > span {
       height: 500px;
       width: 500px;
       padding: 40px;
       background-color: blue;
       display: block;
       color: white;
     }
    </style>
  </head>
  <body>
    <div class="look-for-me">
      The child below
      <span>Look at me, I am the child</span>
    </div>


</body></html>

Actual

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
    <title>Example page</title>
    <meta charset="UTF-8" />

    <style>
     div &gt; span {
       height: 500px;
       width: 500px;
       padding: 40px;
       background-color: blue;
       display: block;
       color: white;
     }
    </style>
  </head>
  <body>
    <div class="look-for-me">
      The child below
      <span>Look at me, I am the child</span>
    </div>


</body></html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example page</title>
<meta charset="UTF-8">
<style>
div > span {
height: 500px;
width: 500px;
padding: 40px;
background-color: blue;
display: block;
color: white;
}
</style>
</head>
<body>
<div class="look-for-me">
The child below
<span>Look at me, I am the child</span>
</div>
</body>
</html>
from selenium import webdriver
# browser = webdriver.Firefox() # works fine
# browser = webdriver.Safari() # works fine
browser = webdriver.Chrome()
browser.get('http://localhost:8000')
browser.implicitly_wait(10)
browser.find_element_by_class_name("look-for-me")
print browser.page_source
browser.quit()
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>Example page</title>
<meta charset="UTF-8" />
<style>
div &gt; span {
height: 500px;
width: 500px;
padding: 40px;
background-color: blue;
display: block;
color: white;
}
</style>
</head>
<body>
<div class="look-for-me">
The child below
<span>Look at me, I am the child</span>
</div>
</body></html>
@ArthurGIT2017
Copy link

This bug has been fixed in the latest ChromeDriver version 74.
Please be aware that starting from ChromeDriver version 74 the compatible Chrome version can be launched by that ChromeDriver, so if you use ChromeDriver version 74 then Chrome version 74 can be launched by that ChromeDriver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment