Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

setAttribute(name,value)语法中, value 如何在语句外定义

环境: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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

字符串拼接呗

'document.getElementById("ContentLeft_menuDate1_TextBox11").setAttribute("value", '+num1+')'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...