首页 > 代码库 > Message: u'$ is not defined' ; Stacktrace
Message: u'$ is not defined' ; Stacktrace
status.html
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>status</title> <script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" /> <script type="text/javascript"> $(document).ready(function(){ $(‘#tooltip‘).tooltip({"placement": "right"}); }); </script> </head> <body> <h3>status</h3> <div class="row-fluid"> <div class="span3"> <input name="user" placeholder="Disabled TextField" disabled /> </div> <div class="span3"> <a class="btn disabled">Disabled Button</a> </div> <div class="span3"> <input name="radio" type="radio" /> </div> </div> </body> <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script> </html>
status.py
# -*- coding: utf-8 -*- from selenium import webdriverfrom time import sleepimport osif ‘HTTP_PROXY‘in os.environ: del os.environ[‘HTTP_PROXY‘]dr = webdriver.Chrome()file_path = ‘file:///‘ + os.path.abspath(‘status.html‘)dr.get(file_path)text_field = dr.find_element_by_name(‘user‘)print text_field.is_enabled()# 直接用enabled?方法去判断该button的话返回的会是true# 这是因为button是使用css方法去disabled的,并不是真正的disable# 这时候需要判断其class里是否有disabled这值来判断其是否处于disable状态print dr.find_element_by_class_name(‘btn‘).is_enabled()# 隐藏掉text_field# 判断其是否显示dr.execute_script(‘$(arguments[0]).hide()‘, text_field)print text_field.is_displayed()# 使用click方法选择raidoradio = dr.find_element_by_name(‘radio‘)radio.click()print radio.is_selected()# 判断元素是否存在try: dr.find_element_by_id(‘none‘)except: print ‘element does not exist‘dr.quit()
执行后会报错,经与乙醇沟通后发现是谷歌被墙导致代码木有完全加载.修改代码如下:
#coing=utf-8from selenium import webdriverfrom time import sleepimport osif ‘HTTP_PROXY‘ in os.environ: del os.environ[‘HTTP_PROXY‘]dr = webdriver.Firefox()file_path = ‘file:///‘ + os.path.abspath(‘status.html‘)dr.get(file_path)text_field = dr.find_element_by_name(‘user‘)print( text_field.is_enabled())print(dr.find_element_by_class_name(‘btn‘).is_enabled())myjs = ‘document.all.user.style.display = "none";‘dr.execute_script(myjs)print( text_field.is_displayed())radio = dr.find_element_by_name(‘radio‘)radio.click()print(radio.is_selected())try: dr.find_element_by_id(‘none‘)except: print(‘element does not exist‘)dr.quit()
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。