栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Python在Selenium Webdriver中通过URL上传图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Python在Selenium Webdriver中通过URL上传图像

您首先需要将图像下载到计算机上,然后将其上传…

您可以使用

requests

import requestsURL = "https://ercess.com//images//events//-Blockchain-2019-36613-banner.png"picture_req = requests.get(URL)if picture_req.status_pre == 200:    with open("/path/to/image.jpg", 'wb') as f:        f.write(picture_req.content)

然后发送

/path/to/image.jpg

driver.find_element(By.XPATH, "//*[@id='upl-zone']/input").send_keys("/path/to/image.jpg")

或者您可以使用

Legacyinterface
urllib
您将使用
urlretrieve

import urllib.requestURL = "https://ercess.com//images//events//-Blockchain-2019-36613-banner.png"urllib.urlretrieve(URL, "file_name.png")driver.find_element(By.XPATH, "//*[@id='upl-zone']/input").send_keys("file_name.png")

编辑:

要使用send_keys将路径发送到文件,您可以使用

pathlib

from pathlib import Path# `cwd`: current directory is straightforwardcwd = Path.cwd()# using "F"string for format you can use: image_file_name = str(cwd) + "" + "file_name.png" image_file_name = fr"{cwd}file_name.png"# this print is just to show the image_file_name   print(image_file_name)

希望这对您有所帮助!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417656.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号