环境:python2.7 selenium
目的:根据选择的年月日信息获得该日的数据信息
代码展示:
from selenium import webdriver
import time
# open web-site
dr = webdriver.Firefox()
dr.get("http://61.163.88.227:8006/hwsq.aspx")
time.sleep(0.5)
# change calendar attribute
# make it editable
js='document.getElementById("ContentLeft_menuDate1_TextBox11").removeAttribute("readonly")'
dr.execute_script(js)
# change it with dynamic value
year = 2010
month = 1
date = 1
num1 = year*10000+month*100+date
print(str(num1))
js='document.getElementById("ContentLeft_menuDate1_TextBox11").setAttribute("value", num1 )'
dr.execute_script(js)
# update desired value
dr.find_element_by_id('ContentLeft_Button1').click()
dr.quit()
报错信息
Traceback (most recent call last):
File "/Users/denggefei/Documents/PycharmProjects/pythonProject1/try.py", line 21, in <module>
dr.execute_script(js)
File "/Users/denggefei/Documents/PycharmProjects/pythonProject1/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "/Users/denggefei/Documents/PycharmProjects/pythonProject1/venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/denggefei/Documents/PycharmProjects/pythonProject1/venv/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: ReferenceError: num1 is not defined
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…