如何使用 Selenium 发送图像?

发布于 2025-01-11 06:33:51 字数 1208 浏览 0 评论 0原文

我正在构建一个 Instagram 聊天机器人,但我不知道如何发送照片。我尝试过 Instagram DM 的工作方式:

text_field = driver.find_element(By.CSS_SELECTOR, "textarea")
text_field.send_keys("/Users/kerq/PycharmProjects/APIs/Astro_images_2/2022-03-02_Record_Prominence_Imaged_by_Solar_Orbiter.jpg")

但是,它所做的只是以字符串形式发送路径。我浏览了 Selenium 的文档,但似乎没有明显的方法可以做到这一点。

另一个选项是找到上传图像按钮并导航到文件,但我不确定如何做到这一点。

这是相关的 HTML:

<svg aria-label="Add Photo or Video" class="_8-yf5 " color="#262626" fill="#262626" height="24" role="img" viewBox="0 0 24 24" width="24"><path d="M6.549 5.013A1.557 1.557 0 108.106 6.57a1.557 1.557 0 00-1.557-1.557z" fill-rule="evenodd"></path><path d="M2 18.605l3.901-3.9a.908.908 0 011.284 0l2.807 2.806a.908.908 0 001.283 0l5.534-5.534a.908.908 0 011.283 0l3.905 3.905" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="2"></path><path d="M18.44 2.004A3.56 3.56 0 0122 5.564h0v12.873a3.56 3.56 0 01-3.56 3.56H5.568a3.56 3.56 0 01-3.56-3.56V5.563a3.56 3.56 0 013.56-3.56z" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path></svg>

非常感谢您的帮助。

I'm building an Instagram chat bot, and I'm not sure how can I send a photo. The way Instagram DMs work, I've tried doing:

text_field = driver.find_element(By.CSS_SELECTOR, "textarea")
text_field.send_keys("/Users/kerq/PycharmProjects/APIs/Astro_images_2/2022-03-02_Record_Prominence_Imaged_by_Solar_Orbiter.jpg")

However, all it does is send the path, as a string. I've looked through Selenium's documentation, but it seems like there's no obvious way to do it.

The other option is to find the upload image button and navigate to the file, but I'm not sure how I would do that.

Here's the relevant HTML:

<svg aria-label="Add Photo or Video" class="_8-yf5 " color="#262626" fill="#262626" height="24" role="img" viewBox="0 0 24 24" width="24"><path d="M6.549 5.013A1.557 1.557 0 108.106 6.57a1.557 1.557 0 00-1.557-1.557z" fill-rule="evenodd"></path><path d="M2 18.605l3.901-3.9a.908.908 0 011.284 0l2.807 2.806a.908.908 0 001.283 0l5.534-5.534a.908.908 0 011.283 0l3.905 3.905" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="2"></path><path d="M18.44 2.004A3.56 3.56 0 0122 5.564h0v12.873a3.56 3.56 0 01-3.56 3.56H5.568a3.56 3.56 0 01-3.56-3.56V5.563a3.56 3.56 0 013.56-3.56z" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path></svg>

I'd appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文