如何使用python中的selenium将图像上载到这个网站

2024-09-24 22:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

每次尝试时,我都会在find_元素的指令中出错,有人可以尝试将图像上载到这个特定的网站:https://www.custojusto.pt/ai/form/0 告诉我怎么解决这个问题?在

def upload():
    driver.get('https://www.custojusto.pt/ai/form/0')
    #time.sleep(10)
    driver.find_element_by_name('image').send_keys("https://images-na.ssl-images-amazon.com/images/I/41pzTMUV7AL._SY300_.jpg")

if __name__ == '__main__':
    upload()

Tags: namehttps图像formpt元素网站www
1条回答
网友
1楼 · 发布于 2024-09-24 22:29:50

首先你可以用我相信的网址上传图片。你需要手动下载然后上传。您可以使用下面线程中讨论的方法下载图像

How to download image using requests

你的上传部分也在IFrame中,所以你需要先切换到IFrame

from selenium import webdriver

driver = webdriver.Chrome()
def upload():
    driver.get('https://www.custojusto.pt/ai/form/0')
    #time.sleep(10)

    driver.switch_to.frame("image-upload")
    driver.find_element_by_name('image').send_keys("/tmp/aws.png")

if __name__ == '__main__':
    upload()
    driver.quit()

我测试了上面的代码,它对我很有用。注意一旦上传了图片,如果你需要上传另一张图片,再次使用driver.switch_to.frame("image-upload"),因为一个新的框架被创建了,旧的不再有效。在

相关问题 更多 >