解决网页元素无法定位(NoSuchElementException: Unable to locate element)的几种方法

Python 2.7
IDE Pycharm 5.0.3

姊妹篇请见解决Selenium弹出新页面无法定位元素问题(Unable to locate element)


只解决一个问题--NoSuchElementException: Message: Unable to locate element

出错形式

这里写图片描述


出错原因

1.可能元素加载未完成

元素加载没完成,同样的路径定位,每次测试结果确是不一样的,有时候抛出错误,有时候正常!这就比较蛋疼了,也就是说,和你的定位方法半毛钱关系没有,而很大程度上取决于你的电脑和网速!

1.解决方案A:添加两行代码

wait = ui.WebDriverWait(driver,10)
wait.until(lambda driver: driver.find_element_by_方法("定位路径自己来"))

WebDriverWait(driver,10)的意思是;10秒内每隔500毫秒扫描1次页面变化,当出现指定的元素后结束。driver是前面操作webdriver.firefox()的句柄
完整的小段代码是:

from selenium import webdriver
import selenium.webdriver.support.ui as ui

driver_item=webdriver.Firefox()
url="https://movie.douban.com/"
wait = ui.WebDriverWait(driver_item,10)
driver_item.get(url)
wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div/div/label[5]"))
driver_item.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div/div/label[5]").click()

1.解决方案B:使用while+try…except结合

下面来个例子,完整的可运行代码如下:

from selenium import webdriver
import time
import os

driver_item=webdriver.Firefox()
url="https://movie.douban.com/"
driver_item.get(url)

while 1:
    start = time.clock()
    try:
        driver_item.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div/div/label[5]").click()
        print '已定位到元素'
        end=time.clock()
        break
    except:
        print "还未定位到元素!"

print '定位耗费时间:'+str(end-start)

运行结果如下:

还未定位到元素!
已定位到元素
定位耗费时间:0.262649990301

分析

开启页面后,并不是元素都一次性加载完成的,依赖于网速和电脑,从B方法中可见,所耗费的时间,还有一种静态的方法就是我以前常用的sleep,一般睡一秒就够了,但是对于不同电脑不同网速的情况,建议还是使用动态方法,也就是A方法,以变应变!

从代码可读性上和效率上都是A方法比较好,更加符合python的特性,简洁优美,而B方法应该是我这样初学者自己能想到的一种方法,先得自己想解决方案,然后再参考现有方法,我感觉这样才有意义。


2.本身定位方法错误

这也就是最常见的了,也是最容易犯的错误,自己对元素定位方法不够熟练,就很容易错误了,所以多想想该怎么定位才最容易,我现在最喜欢的是用xpath方法定位,DOM树的结构挺清晰的,可能我还是新手的原因吧!

2.解决方案

多查询元素定位方法,多使用多熟练,吐槽一下正则。。。相比较正则,我还是更喜欢BeautifulSoup或者xpath来用,额。。。

比方说,我要看看BeautifulSoup到底规则效果怎么样,那我会单独写个测试模块

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import re
#find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等.
# find_parents() 和 find_parent() 用来搜索当前节点的父辈节点,
# 搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

soup=BeautifulSoup(html,"lxml")
#soup=BeautifulSoup(open('index.html'),"lxml")#若本身有html文件,则打开

#print  soup.prettify()
print soup.title
print soup.a.string
print soup.a['href']
print soup.a['class']

这个我就做个比方,什么时候我要用BS4了我会单独测试下自己的规则好不好使,毕竟不是老司机。
这里写图片描述


Tips

1.测试阶段调用Firefox来做,这样更加直观具体,到最后可以调用phantomjs.exe。

2.分模块测试自己的idea,光想远远不够。

3.最好有分屏的电脑,一屏显示代码,一屏显示实现过程


总结

遇到问题看来还是需要一步步来,分析各个模块之间的衔接性和模块完整性,单独把模块拿出来进行测试,不然一个比较大点的程序出错在哪都很费劲,而且,如果是自己写的程序,那么推荐一步步实现,哪一步该有哪一步的功能,这样逻辑才够清晰,这是写给自己的话,如果对你有帮助,我也感到很荣幸!


下篇预告

基本上已经写完豆瓣高分电影及细节的爬取了,也遇到些困难,下篇再说遇到问题和怎么解决


吐槽

哈尔滨好热,,,,学校放假了,都没地方吃饭了,,,,,实验室没几个鸟人。。。。人生不仅需要代码和论文,我还要空调和西瓜!!!
这里写图片描述


致谢

@Eastmount–[Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论

@转–selenium webdriver学习(十一)-怎么等待页面元素加载完成

@阳光总在风雨后–webdriver 的三种等待方式

  • 34
    点赞
  • 106
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Java™ Puzzlers: Traps, Pitfalls, and Corner Cases.chm,英文版本,chm 格式,大小 1 Mb,作者:Joshua Bloch、Neal Gafter。 内容预览: Chapter 1. Introduction Chapter 2. Expressive Puzzlers Puzzle 1: Oddity Puzzle 2: Time for a Change Puzzle 3: Long Division Puzzle 4: It's Elementary Puzzle 5: The Joy of Hex Puzzle 6: Multicast Puzzle 7: Swap Meat Puzzle 8: Dos Equis Puzzle 9: Tweedledum Puzzle 10: Tweedledee Chapter 3. Puzzlers with Character Puzzle 11: The Last Laugh Puzzle 12: ABC Puzzle 13: Animal Farm Puzzle 14: Escape Rout Puzzle 15: Hello Whirled Puzzle 16: Line Printer Puzzle 17: Huh? Puzzle 18: String Cheese Puzzle 19: Classy Fire Puzzle 20: What's My Class? Puzzle 21: What's My Class, Take 2 Puzzle 22: Dupe of URL Puzzle 23: No Pain, No Gain Chapter 4. Loopy Puzzlers Puzzle 24: A Big Delight in Every Byte Puzzle 25: Inclement Increment Puzzle 26: In the Loop Puzzle 27: Shifty i's Puzzle 28: Looper Puzzle 29: Bride of Looper Puzzle 30: Son of Looper Puzzle 31: Ghost of Looper Puzzle 32: Curse of Looper Puzzle 33: Looper Meets the Wolfman Puzzle 34: Down for the Count Puzzle 35: Minute by Minute Chapter 5. Exceptional Puzzlers Puzzle 36: Indecision Puzzle 37: Exceptionally Arcane Puzzle 38: The Unwelcome Guest Puzzle 39: Hello, Goodbye Puzzle 40: The Reluctant Constructor Puzzle 41: Field and Stream Puzzle 42: Thrown for a Loop Puzzle 43: Exceptionally Unsafe Puzzle 44: Cutting Class Puzzle 45: Exhausting Workout Chapter 6. Classy Puzzlers Puzzle 46: The Case of the Confusing Constructor Puzzle 47: Well, Dog My Cats! Puzzle 48: All I Get Is Static Puzzle 49: Larger Than Life Puzzle 50: Not Your Type Puzzle 51: What's the Point? Puzzle 52: Sum Fun Puzzle 53: Do Your Thing Puzzle 54: Null and Void Puzzle 55: Creationism Chapter 7. Library Puzzlers Puzzle 56: Big Problem Puzzle 57: What's in a Name? Puzzle 58: Making a Hash of It Puzzle 59: What's the Difference? Puzzle 60: One-Liners Puzzle 61: The Dating Game Puzzle 62: The Name Game Puzzle 63: More of the Same Puzzle 64: The Mod Squad Puzzle 65: A Strange Saga of a Suspicious Sort Chapter 8. Classier Puzzlers Puzzle 66: A Private Matter Puzzle 67: All Strung Out Puzzle 68: Shades of Gray Puzzle 69: Fade to Black Puzzle 70: Package Deal Puzzle 71: Import Duty Puzzle 72: Final Jeopardy Puzzle 73: Your Privates Are Showing Puzzle 74: Identity Crisis Puzzle 75: Heads or Tails? A Glossary of Name Reuse Chapter 9. More Library Puzzlers Puzzle 76: Ping Pong Puzzle 77: The Lock Mess Monster Puzzle 78: Reflection Infection Puzzle 79: It's a Dog's Life Puzzle 80: Further Reflection Puzzle 81: Charred Beyond Recognition Puzzle 82: Beer Blast Puzzle 83: Dyslexic Monotheism Puzzle 84: Rudely Interrupted Puzzle 85: Lazy Initialization Chapter 10. Advanced Puzzlers Puzzle 86: Poison-Paren Litter Puzzle 87: Strained Relations Puzzle 88: Raw Deal Puzzle 89: Generic Drugs Puzzle 90: It's Absurd, It's a Pain, It's Superclass! Puzzle 91: Serial Killer Puzzle 92: Twisted Pair Puzzle 93: Class Warfare Puzzle 94: Lost in the Shuffle Puzzle 95: Just Desserts Appendix A. Catalog of Traps and Pitfalls 1. Lexical Issues 2. Integer Arithmetic 3. Floating-Point Arithmetic 4. Expression Evaluation 5. Flow of Control 6. Class Initialization 7. Instance Creation and Destruction 8. Other Class- and Instance-Related Topics 9. Name Reuse 10. Strings 11. I/O 12. Threads 13. Reflection 14. Serialization 15. Other Libraries Appendix B. Notes on the IllusionsAmbiguous Figures Impossible Figures Geometrical Illusions: Size Geometrical Illusions: Direction Subjective Contours Anomalous Motion Illusions Illusions of Lightness Compound Illusions

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值