Skip to content

Instantly share code, notes, and snippets.

@andy23512
Last active March 9, 2018 23:56
Show Gist options
  • Save andy23512/a739dcdbafc30edf7ff85cc3ffee1dd4 to your computer and use it in GitHub Desktop.
Save andy23512/a739dcdbafc30edf7ff85cc3ffee1dd4 to your computer and use it in GitHub Desktop.
get code in react codemirror via selenium
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"code1\": \"<header class=\\\"site-header\\\">\\n <div class=\\\"container\\\">\\n <h1>Example #2</h1>\\n <nav role=\\\"navigation\\\" class=\\\"site-navigation\\\">\\n <ul>\\n <li><a href=\\\"#\\\">Link</a></li>\\n <li><a href=\\\"#\\\">Link</a></li>\\n <li><a href=\\\"#\\\">Link</a></li>\\n </ul>\\n </nav>\\n </div>\\n</header>\\n<section role=\\\"main\\\" class=\\\"container\\\"> <img src=\\\"http://placehold.it/1400x400/ff694d/f6f2eb\\\" class=\\\"banner-image\\\" />\\n <div class=\\\"grid-row col-3\\\">\\n <div class=\\\"grid-unit\\\"> <img src=\\\"http://placehold.it/650x300/ff694d/f6f2eb\\\" />\\n <p>Nullam quis risus eget urna mollis ornare vel eu leo. Donec id elit non mi porta gravida at eget metus. Curabitur blandit tempus porttitor. </p>\\n </div>\\n <div class=\\\"grid-unit\\\"> <img src=\\\"http://placehold.it/650x300/ff694d/f6f2eb\\\" />\\n <p>Nullam quis risus eget urna mollis ornare vel eu leo. Donec id elit non mi porta gravida at eget metus. Curabitur blandit tempus porttitor. </p>\\n </div>\\n <div class=\\\"grid-unit\\\"> <img src=\\\"http://placehold.it/650x300/ff694d/f6f2eb\\\" />\\n <p>Nullam quis risus eget urna mollis ornare vel eu leo. Donec id elit non mi porta gravida at eget metus. Curabitur blandit tempus porttitor. </p>\\n </div>\\n </div>\\n</section>\",\n",
" \"lineCount1\": 25,\n",
" \"code2\": \"aaa\",\n",
" \"lineCount2\": 1\n",
"}\n"
]
}
],
"source": [
"# -*- coding: utf-8 -*-\n",
"\n",
"from selenium import webdriver\n",
"\n",
"browser = webdriver.Chrome('./chromedriver')\n",
"\n",
"browser.get('https://scniro.github.io/react-codemirror2/')\n",
"\n",
"# steal react instance (https://stackoverflow.com/a/39165212)\n",
"code = browser.execute_script(\"\"\"\n",
" window.FindReact = function(dom) {\n",
" for (var key in dom) {\n",
" if (key.startsWith(\"__reactInternalInstance$\")) {\n",
" return dom[key];\n",
" }\n",
" }\n",
" return null;\n",
" };\n",
" var someElement = document.getElementsByClassName('react-codemirror2')[0];\n",
" var reactState = FindReact(someElement).return.stateNode; \n",
" var code1 = reactState.editor.getValue();\n",
" var lineCount1 = reactState.editor.lastLine() + 1;\n",
" reactState.initChange('aaa');\n",
" var code2 = reactState.editor.getValue();\n",
" var lineCount2 = reactState.editor.lastLine() + 1;\n",
" return JSON.stringify({code1: code1, lineCount1: lineCount1, code2: code2, lineCount2: lineCount2}, null, 2);\n",
"\"\"\")\n",
"\n",
"print(code)\n",
"\n",
"browser.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment