携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情
之前写过一篇抓取slack联系人的文章(python自动化神器专题3:slack自动化, 最好用的桌面自动化开发工具。
今天再写一篇自动化操作slack的文章,通过web登录slack,自动化给某个频道发送消息。 如果需要发送一些产品营销的信息,这个会很有用。
需求说明
向 Slack 社区的频道发送消息。 我们可以使用 Clicknium 快速开始这个简单的初学者过程。
环境准备
- Windows 10
- Visual Studio Code 1.69.2
- Clicknium 0.1.3
- Python 3.10.5
- Chrome
- Python package pyperclip
Remarks:
- Need run this sample in English region.
运行这个示例
- 按照【clicknium入门】(www.clicknium.com/documents/q…
- Clone sample repo.
git clone https://github.com/automation9417/automation-samples.git - 在 Visual Studio 代码中打开文件夹“WebSlackSendMessage”
- 在 Visual Studio 代码中打开“sample.py”。
- 在
sample.py中填写标志配置sign_method_name="" #google for Google account, slack_email for slack email. sign_in_email_or_phone="" #google email or slack email sign_in_password="" #account passwword - 在
sample.py中填写 slack 配置slack_community_url="" #The URL of the slack community you want to send essage. e.g."https://example.slack.com" slack_channel_name="" #The name of the channel you want to send message. slack_message="" #The message content - 按“F5”调试示例或按“CTRL+F5”运行示例。
步骤
- 假设 Slack 没有在 chrome 中打开,所以我们需要先用社区地址打开 chrome。
#Use following code to open chrome with target url browser_tab=clicknium.chrome.open("https://example.slack.com") # update the address to your slack community. - 假设Slack没有登录,所以我们需要用谷歌账号或者Slack账号登录slack。
- Google 账号登录
from msilib.schema import Error from clicknium import clicknium, locator def google_sign_in(email,password): clicknium.find_element(locator.websites.slack.google_sign_in_btn).click() choose_account_lebel=clicknium.wait_appear(locator.websites.google_account.choose_account_label,wait_timeout=5) if choose_account_lebel: clicknium.find_element(locator.websites.google_account.use_another_account_btn).click() email_or_phone_input=clicknium.wait_appear(locator.websites.google_account.email_or_phone_input,wait_timeout=5) if email_or_phone_input: email_or_phone_input.set_text(email) else: error_msg="email_or_phone_input not found." raise Error(error_msg) clicknium.find_element(locator.websites.google_account.email_or_phone_next_btn).click() password_input=clicknium.wait_appear(locator.websites.google_account.password_input,wait_timeout=5) if password_input: password_input.set_text(password) else: error_msg="password_input not found." raise Error(error_msg) clicknium.find_element(locator.websites.google_account.password_next_btn).click() - Slack 账号登录
from clicknium import clicknium, locator def slack_email_sign_in(email,password): clicknium.find_element(locator.websites.slack.slack_email_input).set_text(email) clicknium.find_element(locator.websites.slack.slack_password_input).set_text(password) clicknium.find_element(locator.websites.slack.slack_signin_btn).click()
- 取消打开slack桌面应用
- 点击
Cancel按钮from clicknium import clicknium, locator from clicknium.common.enums import * def close_open_desk(): open_slack_cancel_btn= clicknium.wait_appear(locator.desktops.chrome.open_slack_win_cancel_btn,wait_timeout=10) if open_slack_cancel_btn: open_slack_cancel_btn.click(by=MouseActionBy.MouseEmulation)
- 选择 'use slack in browser'
- 点击
use Slack in your browserfrom clicknium import clicknium, locator def use_slack_in_browser(): use_slack_in_browser_button=clicknium.wait_appear(locator.websites.slack.use_slack_in_browser_button,wait_timeout=5) if use_slack_in_browser_button: use_slack_in_browser_button.click()
- 打开频道搜索页.
- 发送快捷键
Ctrl+Shift+L,打开频道搜索页面def browse_channels(): channels_menu_inner_span=clicknium.wait_appear(locator.websites.app_slack.channels_menu_inner_span,wait_timeout=5) if channels_menu_inner_span: clicknium.send_hotkey("^+l") sleep(1) else: msg="channels menu not found." raise Error(msg)
- 搜索并选择目标频道。
- 输入目标频道名称
- 单击“搜索”图标
- 选择排序“A到Z”
- 选择目标频道
from msilib.schema import Error from clicknium import clicknium, locator def search_and_select_channel(channel_name): clicknium.find_element(locator.websites.app_slack.search_channel_tbx).set_text(channel_name) clicknium.find_element(locator.websites.app_slack.search_channel_btn).click() clicknium.find_element(locator.websites.app_slack.channel_sort_btn).click() clicknium.find_element(locator.websites.app_slack.sort_atoz_btn).click() matched_result_span=clicknium.wait_appear(locator.websites.app_slack.matched_result_span,{"channel_name": channel_name}) if matched_result_span: matched_result_span.click() else: msg="No matched channel for "+channel_name raise Error(msg)
- 输入信息并发送。
- 输入消息
- 点击“发送”图标
from clicknium import clicknium, locator import pyperclip def send_message(channel_name, message): navigate_to_browser_channel_page() search_and_select_channel(channel_name) clicknium.find_element(locator.websites.app_slack.channel_message_input).set_focus() clicknium.send_hotkey('^a') clicknium.send_hotkey('^x') pyperclip.copy(message) clicknium.send_hotkey('^v') clicknium.find_element(locator.websites.app_slack.send_message_btn).click()
- 退出。
- 点击用户头像
- 点击“退出”
```python
from clicknium import clicknium, locator
def sign_out():
user_avatar_btn=clicknium.wait_appear(locator.websites.app_slack.user_avatar_btn,wait_timeout=5)
if user_avatar_btn:
user_avatar_btn.click()
clicknium.find_element(locator.websites.app_slack.sign_out_btn).click()
```
9. 关闭打开的浏览器选项卡。
browser_tab.close()# close the opened browser tab.
# 提示
- 将变量传递给定位器 在此示例中,通道名称被传递给“matched_result_span”定位器,如下所示
- 在定位器中定义变量
- Pass variable in code
matched_result_span=clicknium.wait_appear(locator.websites.app_slack.matched_result_span,{"channel_name": channel_name}) - 在定位器中使用通配符
在此示例中,
channel_sort_btn定位器的 sInfo 以 * 结尾更新,如下所示