最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【已解决】Python的Selenium中直接运行会报错:selenium.common.exceptions.NoSuchElementException Message no such element Unable to locate element

Python crifan 1301浏览 0评论

折腾:

【已解决】Python中Selenium中如何实现模拟点击按钮

期间,一行行调试代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get(url)
print  “driver.title=%s”%(driver.title)
assert u”百度” in driver.title
searchElement = driver.find_element_by_name(“wd”)
searchElement.send_keys(“crifan”)
searchElement.send_keys(Keys.RETURN)
print  “searchElement=%s”%(searchElement)
# print driver.page_source
nextPageElement = driver.find_element_by_class_name(“n”)
print  “nextPageElement=%s”%(nextPageElement)
nextPageElement.send_keys(Keys.ENTER)

是可以正常运行,不会报错的。

但是直接运行,没有调试(的暂停,的等待时间),则就会报错:

searchElement=<selenium.webdriver.remote.webelement.WebElement (session=”1e22e51571ade5e2b5d0b513a4281f47″, element=”0.034347571190473936-1″)>
Traceback (most recent call last):
  File “/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py”, line 1596, in <module>
    globals = debugger.run(setup[‘file’], None, None, is_module)
  File “/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py”, line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File “/Users/crifan/dev/dev_root/projects/亚马逊/自动下单/AutoOrder/AutoOrder.py”, line 17, in <module>
    nextPageElement = driver.find_element_by_class_name(“n”)
  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 485, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 855, in find_element
    ‘value’: value})[‘value’]
  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 308, in execute
    self.error_handler.check_response(response)
  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py”, line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:”class name”,”selector”:”n”}
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.12.6 x86_64)

很明显,页面没有加载完毕,就运行了,而调试期间的间隔时间,则是等页面加载完毕,所以不会报错,可以找到元素的。

别人这种:

selenium基础知识六 – 键盘事件 – 十五言

直接加sleep,虽然临时可以,但是效率太低,而且不够稳定和严谨

selenium.common.exceptions.NoSuchElementException: Message no such element: Unable to locate element

python – selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element – Stack Overflow

python – NoSuchElementException: Message: Unable to locate element: {“method”:”link text”,”selector”:”Sign in”} – Stack Overflow

可以这样写click的:

<code>driver.find_element_by_xpath("//div[. = 'Sign in']").click()
driver.find_element_by_id("signin").click()
</code>

Python with selenium: unable to locate element which really exist – Stack Overflow

webdriver – selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: – Stack Overflow

好像这个WebDriverWait思路是对的:

去找找

python – NoSuchElementException: Message: Unable to locate element: [id=”j_username”] attempting WebdriverWait – Stack Overflow

selenium wait until

WebDriver: Advanced Usage — Selenium Documentation

5. Waits — Selenium Python Bindings 2 documentation

java – How to wait until an element is present in Selenium? – Stack Overflow

http://toolsqa.com/selenium-webdriver/wait-commands/

【总结】

通过代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import time
driver = webdriver.Chrome()
driver.get(url)
print  “driver.title=%s”%(driver.title)
assert u”百度” in driver.title
searchElement = driver.find_element_by_name(“wd”)
searchElement.send_keys(“crifan”)
searchElement.send_keys(Keys.RETURN)
print  “searchElement=%s”%(searchElement)
# print driver.page_source
try:
    print “before wait: %s”%(time.strftime(‘%Y%m%d %H:%M:%S’,time.localtime(time.time())))
    nextPageElement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, “n”)))
    print “after wait: %s”%(time.strftime(‘%Y%m%d %H:%M:%S’,time.localtime(time.time())))
except:
    print u”can not find 下一页”
    driver.quit()
# nextPageElement = driver.find_element_by_class_name(“n”)
print  “nextPageElement=%s”%(nextPageElement)
nextPageElement.send_keys(Keys.ENTER)

然后就可以正常的,用WebDriverWait等到页面去加载,直到出现了class name为n的“下一页”的按钮:

通过log:

driver.title=百度一下,你就知道
searchElement=<selenium.webdriver.remote.webelement.WebElement (session=”487c5c8eecf619a6fd4216e30d617eb3″, element=”0.1463384739209479-1″)>
before wait: 20171109 23:18:00
after wait: 20171109 23:18:01
nextPageElement=<selenium.webdriver.remote.webelement.WebElement (session=”487c5c8eecf619a6fd4216e30d617eb3″, element=”0.1463384739209479-2″)>

可以看出:

此处从百度搜索页面结果显示,到显示出下一页,都在1秒左右(以内)

-》换句话说,程序的代码去处理的话,都还是很快的

然后再模拟点击,加载下一页了:

转载请注明:在路上 » 【已解决】Python的Selenium中直接运行会报错:selenium.common.exceptions.NoSuchElementException Message no such element Unable to locate element

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.268 seconds, using 12.04MB memory