Message: u'The given selector btn dropdown-toggle btn-info is either invalid or does not result in a...

 

html代码:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title>button dropdown</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(){
                    $('.dropdown-menu').find('a').first().click(function(){ alert('watir-webdriver is better than selenium-webdriver'); });
                }
            );
        </script>
    </head>
    <body>
        <h3>button dropdown</h3>
        <div class="row-fluid">
            <div class="span3">     
                <div class="well">
                    <div class="btn-group">
                        <a class="btn dropdown-toggle btn-info" data-toggle="dropdown" href="#">
                            Info
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a href="#">watir-webdriver</a></li>
                            <li><a href="#">better than</a></li>
                            <li><a href="#">selenium-webdriver</a></li>
                        </ul>
                    </div>
                </div>          
            </div>      
        </div>      
    </body>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>

  

Python 代码:

#coding=utf-8

from selenium import  webdriver
from selenium.webdriver.common.keys import Keys
from time import  sleep
import  os

if 'HTTP_PROXY' in os.environ: del os.environ['HTTP_PROXY']

dr = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('button_dropdown.html')
dr.get(file_path)

sleep(1)

#
dr.find_element_by_class_name('btn dropdown-toggle btn-info').click()
buttons =dr.find_elements_by_class_name('dropdown-menu')
for btn in buttons:
    if btn.text == 'better than': btn.click()

sleep(1)

dr.quit()

  错误信息:InvalidSelectorException: Message: u'The given selector btn dropdown-toggle btn-info is either invalid or does not result in a WebElement.

 

解决办法:

 

#coding=utf-8

from selenium import  webdriver
from selenium.webdriver.support.ui import  WebDriverWait
from time import  sleep
import  os

if 'HTTP_PROXY' in os.environ: del os.environ['HTTP_PROXY']

dr = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('button_dropdown.html')
dr.get(file_path)

sleep(1)

#点击下拉菜单
dr.find_element_by_link_text('Info').click()

#找到dropdown-menu父元素
WebDriverWait(dr,10).until(lambda the_driver: the_driver.find_element_by_class_name('dropdown-menu').is_displayed())

#找到better than
menu = dr.find_element_by_class_name('dropdown-menu').find_element_by_link_text('better than')

menu.click()

sleep(3)

dr.quit()

  

转载于:https://www.cnblogs.com/xiaobaichuangtianxia/p/3736925.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值