Python selenium - modifying the source code of a webpage

前端 未结 3 1601
暗喜
暗喜 2021-02-10 06:15

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 m

3条回答
  •  醉酒成梦
    2021-02-10 06:36

    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';
    
      
        

    [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?

提交回复
热议问题