Skip to main content

The loop waits until the selenium element has loaded.

Project description

Swaiter

​ 项目名字取自:Selenium+waiter

​ 作为一个爬虫人,在使用selenium进行爬虫时,时长会遇到由于网络等原因导致无法及时获取一些元素,从而导致程序报错。此时聪明的小伙伴就会说了:加个time.sleep()不就好了。事实上这个方法确实可以解决绝大部分问题,但其也有一个比较尴尬的问题:到底要“睡”多久合适?长了耽误爬取效率,短了又怕没有作用,所以这个库就是为了解决上述问题而存在的。

安装

库的安装十分简单直接使用以下面的命令就可以安装成功了:

pip install Swaiter

使用

使用方法主要分为两种:直接使用基础组件进行开发、继承并添加自己的检索函数

使用基础组件

这里给一个简单的使用基础组件的例子:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from waiter import Waiter

if __name__ == '__main__':
    # 获取selenium中驱动配置项(可省略)
    driver_opt = Options()
    url = "https://www.baidu.com/"
    # 获取selenium中的driver对象
    chrome_driver = Chrome(options=driver_opt)
    chrome_driver.get(url=url)
    # 利用获取到的driver对象创建Waiter对象
    w = Waiter(chrome_driver)
    # 传入xpath路径以及查询参数,检索input对象,并向其中填入查询参数
    w.input_send_waiter(xpath="//input[@id='kw']", query="python")
    # 利用xpath找到点击对象,模拟点击
    w.click_waiter(xpath="//input[@id='su']")
    # 在新的页面找到第一个链接,并获取其中的href属性值
    result = w.elements_text_waiter(xpath="//h3/a[@tabindex=0]", attribute="href")
    print(result)

​ 上述例子中,使用了3个基础的搜索函数,向这样的搜索函数,本库一共提供了5个,应该可以满足简单的模拟需求,当然如果你有一些特别的搜索需求,也可以尝试使用第二种方式。

继承并开发自己的检索函数

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

class MyWaiter(Waiter):
    def __init__(self, driver, interval, is_track, is_log):
        super().__init__(driver=driver, interval=interval, is_track=is_track, is_log=is_log)

    def _textarea_send(self, xpath, **kwargs):
        self.driver.find_element(By.XPATH, xpath).send(kwargs)

    def textarea_send(self, xpath, **kwargs):
        self.wait(func="_textarea_send", xpath=xpath, interval=5, **kwargs)

环境

window10、window11

python>=3.8

selenium>=4.9.0

loguru>=0.7.0

版本

2023.5.22

Version-1.0.4

  • 添加select组件的搜索器
    • 添加获取组件所有选项
    • 添加向select组件中输入值

1.0.4

2023.5.16

Version-1.0.3

完成基础功能

1.0.3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Swaiter-1.0.4.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Swaiter-1.0.4-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file Swaiter-1.0.4.tar.gz.

File metadata

  • Download URL: Swaiter-1.0.4.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for Swaiter-1.0.4.tar.gz
Algorithm Hash digest
SHA256 f1a2fcd757a8746b8b54e694086236c1e28435f5f1101b5f696879c03db56317
MD5 8d38fc6c519fea136892134d63ffa3be
BLAKE2b-256 1f5fe48b8b695b202c0b459e9785714e321e64fa5f1baf48b0ff7e67f7a8367c

See more details on using hashes here.

File details

Details for the file Swaiter-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: Swaiter-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for Swaiter-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 11f85c68c0edd5c53fb57c708a12926eb4fb128ea9c826887e0ae8831195fe9c
MD5 9c497d6cbdcafb393376b53806c8c399
BLAKE2b-256 45c7c828b3c8a87414a47139e8bcf60c1fa249024ccf0ad5508bb56ae57c5768

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page