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()

  

Message: u'The given selector btn dropdown-toggle btn-info is either invalid or does not result in a WebElement的更多相关文章

  1. Appium 运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for (转)

    现象:Appium运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported f ...

  2. RobotFramework+Appium 升级Appium v1.10.0后,执行click element时报错:InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for this session,解决办法

    报错信息如下: debug] [35m[XCUITest][39m Connection to WDA timed out[debug] [35m[XCUITest][39m Connection t ...

  3. jquery $('#btn').click与$("#btn").live("click",function()有什么区别?

    live方法绑定的事件处理函数,在页面中未来添加的元素只要满足原来的选择器,仍然会导致事件触发.普通的事件绑定则没有这个效果.对于#btn这个选择器来说,如果你未来将id为btn的元素删除,然后再创建 ...

  4. Bootstrap4总结

    一. bootstrap简介 Bootstrap,来自 Twitter(全国最大的微博),是目前最受欢迎的前端框架. bootstrap下载及演示 http://v3.bootcss.com 什么是b ...

  5. Bootstrap 下拉菜单和滚动监听插件

    一.下拉菜单 常规使用中,和组件方法一样,代码如下: //声明式用法 <div class="dropdown"> <button class="btn ...

  6. bootstrap的滚动监听

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  7. python+selenium自动化软件测试(第2章):WebDriver API

    2.1 操作元素基本方法 前言前面已经把环境搭建好了,从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可 ...

  8. Selenium2+python自动化73-定位的坑:class属性有空格

    前言 有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message: The given selector u-label f-dn ...

  9. 2.33 定位的坑:class属性有空格

    2.33 定位的坑:class属性有空格 前言有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message:The given sel ...

随机推荐

  1. Ubuntu-14.04.1 desktop安装时遇到的小问题

    su root认证失败:sudo passwd root,然后设置新密码. 重装linux导致g++显示已安装,但无法使用:将"系统设置"/"软件源"中所有更新 ...

  2. ubuntu 命令汇总

    1.linux添加全局变量 //查看当前的全局变量 echo $PATH //打开bashrc 文件,在最后面添加需要 加入的目录路径 vi ~/.bashrc //例如: export PATH=$ ...

  3. 【转】ECharts3.x中的点击事件与行为

    在ECharts中主要通过 on 方法添加事件处理函数,ECharts中的事件主要分为两种,1)鼠标事件,在鼠标click  or  hove 时触发鼠标事件: 2)另外一种是在ECharts在做图形 ...

  4. springboot ssl http转Https

    参考:https://www.cnblogs.com/imfjj/p/9058443.html   (里面有坑) https://blog.csdn.net/l4642247/article/deta ...

  5. Java工具类实现校验公民身份证的有效性

    转自:https://www.oschina.net/code/snippet_1859292_39120 1 package com.tg.user.controller; import java. ...

  6. Java System.getProperty("java.io.tmpdir") 获取系统临时目录

    System.getProperty("java.io.tmpdir") 是获取操作系统的缓存临时目录 在windows7中的目录是: C:\Users\登录用户~1\AppDat ...

  7. favicon.ico

    favicon.ico 作为网页的图标,被当前的所有浏览器都支持. 可直接放在主目录下,自动加载,也可设置在header中. <link rel="shortcut icon" ...

  8. Algorithm-多目标优化-博文路径

    参考博文: 多目标进化算法(MOEA)概述: https://blog.csdn.net/qithon/article/details/72885053 多目标优化问题的算法及其求解: https:/ ...

  9. 编写jQuery插件(一)——插件约定及插件中的闭包

    编写插件的目的是给已经有的一系列方法或函数做一个封装,以便在其他地方重复使用,提高开发效率和方便后期维护. 在编写jQuery插件的时候,我们一般会遵循一些约定: jQuery插件推荐命名为:jque ...

  10. Haskell语言学习笔记(42)Bifunctor

    Bifunctor class Bifunctor p where bimap :: (a -> b) -> (c -> d) -> p a c -> p b d bim ...