Python selenium - modifying the source code of a webpage
<p>I am using Python selenium to automate my attendance entry. It was working fine, now I wanted to try by modifying the source code. I have seen few posts stating that it can be modified using <code>driver.execute_script()</code> and it works for JavaScript, but in my case I need to modify a source code under the <code>select</code> tag. I was able to modify the source code using the <code>inspect element</code>. The following is <code>select</code> tags source code:</p>

<pre><code>&lt;select name="date1"&gt;
    &lt;option value="2016-09-17"&gt;2016-09-17&lt;/option&gt;
    &lt;option value="2016-09-16"&gt;2016-09-16&lt;/option&gt;
    &lt;option value="2016-09-14"&gt;2016-09-14&lt;/option&gt;
&lt;/select&gt;
</code></pre>

<p>I tried to do it with <code>driver.execute_script()</code>. The following was my code:</p>

<pre><code>sel = driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/form/table/tbody/tr[2]/td[3]/select')
input_list = sel.find_element_by_tag_name('option')
cmd = "input_list.value = '2016-09-07'"
driver.execute_script(cmd)
</code></pre>

<p>But the above code is giving me the following error:</p>

<blockquote>
  <p>selenium.common.exceptions.WebDriverException: Message: input_list is not defined</p>
</blockquote>

<p>I am able to modify the source code using the <code>inspect element</code> window. Is there any way to modify the source code using selenium? </p>

<p>The problem is that <code>execute_script</code> executes JavaScript inside the browser [1], which knows nothing about python variables in python script. In particuar <code>input_list</code> is not defined for JavaScript, since it's a python variable.</p>

<p>To fix this, you can select the element inside the JavaScript file. To do this, you can set your cmd to something like this [2]:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>    function getElementByXpath(path) {
      return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }

    getElementByXpath("/html/body/div[3]/div/div[2]/form/table/tbody/tr[2]/td[3]/select/option[1]").value = '2016-09-07';</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;
  &lt;body&gt;
    &lt;div&gt;&lt;/div&gt;
    &lt;div&gt;&lt;/div&gt;
    &lt;div&gt;
      &lt;div&gt;
        &lt;div&gt;&lt;/div&gt;
        &lt;div&gt;
          &lt;form&gt;
            &lt;table&gt;
              &lt;tbody&gt;
                &lt;tr&gt;&lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;
                    &lt;select name="date1"&gt;
                      &lt;option value="2016-09-17"&gt;2016-09-17&lt;/option&gt;
                      &lt;option value="2016-09-16"&gt;2016-09-16&lt;/option&gt;
                      &lt;option value="2016-09-14"&gt;2016-09-14&lt;/option&gt;
                    &lt;/select&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
              &lt;/tbody&gt;
            &lt;/table&gt;
          &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    </code></pre>
</div>
</div>
</p>

<p>[1] <a href="https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_script" rel="nofollow">https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_script</a></p>

<p>[2] <a href="https://stackoverflow.com/questions/10596417/is-there-a-way-to-get-element-by-xpath-using-javascript-in-selenium-webdriver">Is there a way to get element by Xpath using JavaScript in Selenium WebDriver?</a></p>

python selenium