6

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 driver.execute_script() and it works for JavaScript, but in my case I need to modify a source code under the select tag. I was able to modify the source code using the inspect element. The following is select tags source code:

<select name="date1">
    <option value="2016-09-17">2016-09-17</option>
    <option value="2016-09-16">2016-09-16</option>
    <option value="2016-09-14">2016-09-14</option>
</select>

I tried to do it with driver.execute_script(). The following was my 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)

But the above code is giving me the following error:

selenium.common.exceptions.WebDriverException: Message: input_list is not defined

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

3 Answers 3

7

Try following solution and let me know if any issues occurs:

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

P.S. I advise you not to use absolute XPath in your selectors, but relative instead

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. It worked. There was a small error in the code, I edited it.
3

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

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]:

    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';
<html>
  <body>
    <div></div>
    <div></div>
    <div>
      <div>
        <div></div>
        <div>
          <form>
            <table>
              <tbody>
                <tr></tr>
                <tr>
                  <td></td>
                  <td></td>
                  <td>
                    <select name="date1">
                      <option value="2016-09-17">2016-09-17</option>
                      <option value="2016-09-16">2016-09-16</option>
                      <option value="2016-09-14">2016-09-14</option>
                    </select>
                  </td>
                </tr>
              </tbody>
            </table>
          </form>
        </div>
      </div>
    </div>
    

[1] https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_script

[2] Is there a way to get element by Xpath using JavaScript in Selenium WebDriver?

1 Comment

Thanks a lot, but the script suggested by Anderson worked without any additional JavaScript.
1

in python use this :

element = driver.find_element_by_id("some-random-number")
driver.execute_script("arguments[0].innerText = 'change text'", element)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.