Skip to main content
 首页 » 编程设计

python之如何在 python 中使用 selenium 将图像上传到这个网站

2025年02月15日6wuhuacong

每次尝试时,我都会在 find_element 指令中出错,有人可以尝试将图像上传到这个特定的网站: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() 

请您参考如下方法:

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

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"),因为新框架已创建,旧框架不再有效。