Selenium使用注意事项:

find_element 和 find_elements 的区别

WebDriver和WebElement的区别

问题:

会遇到报错:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="1"]"}

NoSuchElementException 的意思就是在当前的网页上找不到该元素, 就是找不到id为1的元素。

分析原因:

可以用sleep等待时间来解决

from selenium import webdriver
from selenium.webdriver.common.by import By

wd = webdriver.Chrome()

wd.get('https://www.byhy.net/_files/stock1.html')

element.send_keys('通讯\n')

element = wd.find_element(By.ID, 'go')
element.click()

import time

while True:
    try:
        element=wd.find_element(By.ID,'1')
        print(element.txt)
        break
    except:
        time.sleep(1)

wd.quit()

但是这样的解决方案存在问题:

Selenium的Webdriver对象有个方法叫 implicitly_wait ,可以称之为隐式等待 ,或者全局等待 。该方法接受一个参数, 用来指定最大等待时长。

wd.implicitly_wait(10)

最后

from selenium import webdriver
from selenium.webdriver.common.by import By

wd = webdriver.Chrome()
wd.implicitly_wait(10)

wd.get('https://www.byhy.net/_files/stock1.html')

element = wd.find_element(By.ID, 'kw')

element.send_keys('通讯\n')


# 返回页面 ID为1 的元素
element = wd.find_element(By.ID,'1')

print(element.text)

相关推荐

  1. Docker使用注意事项

    2024-07-10 13:08:01       13 阅读
  2. 【Qt】UDP使用注意事项

    2024-07-10 13:08:01       22 阅读
  3. C++使用模板的注意事项

    2024-07-10 13:08:01       33 阅读
  4. mysql中使用IN的注意事项

    2024-07-10 13:08:01       42 阅读
  5. scss 使用变量名注意事项

    2024-07-10 13:08:01       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 13:08:01       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 13:08:01       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 13:08:01       4 阅读
  4. Python语言-面向对象

    2024-07-10 13:08:01       5 阅读

热门阅读

  1. 精通C#编程需要学习哪些常用框架?

    2024-07-10 13:08:01       8 阅读
  2. Redis高可用解决方案哨兵模式与集群模式的比较

    2024-07-10 13:08:01       7 阅读
  3. C#实用的工具类库

    2024-07-10 13:08:01       10 阅读
  4. 4085行代码还原2D我的世界(上)

    2024-07-10 13:08:01       9 阅读
  5. 大数据面试题之GreenPlum(1)

    2024-07-10 13:08:01       10 阅读
  6. 量化机器人能否识别市场机会?

    2024-07-10 13:08:01       9 阅读
  7. 探讨SpringMVC的工作原理

    2024-07-10 13:08:01       10 阅读
  8. CSS布局艺术:掌握水平与垂直对齐的秘诀

    2024-07-10 13:08:01       7 阅读
  9. SQL 游标

    2024-07-10 13:08:01       8 阅读
  10. 0706_ARM8

    2024-07-10 13:08:01       11 阅读