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>Try following solution and let me know if any issues occurs:</p>

<pre><code>driver.execute_script("""document.querySelector("select[name='date1'] option").value="2016-09-07";""")
</code></pre>

<p>P.S. I advise you not to use absolute <code>XPath</code> in your selectors, but relative instead</p>

python selenium