无法在selenium python中上传图片

时间:2018-04-04 12:32:04

标签: python selenium

我正在使用的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time

wait = WebDriverWait
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options,
                          executable_path=r'C:\\chromedriver\\chromedriver.exe')
driver.maximize_window()
driver.get("https://www.arttoframe.com/canvas_acrylic/")
driver.switch_to_frame("Uploadimage")
driver.find_element_by_xpath('//*[ @ id = "dropTarget"] / img').send_keys("C:\\Users\\Dell\\Downloads\\340.JPG")

错误:

  

C:\用户\戴尔\ PycharmProjects \ ATF_TestOrder \ VENV \脚本\ python.exe   C:/Users/Dell/PycharmProjects/ATF_TestOrder/ATF_TestOrder/ATF_TestOrder.py   Traceback(最近一次调用最后一次):文件   “C:/Users/Dell/PycharmProjects/ATF_TestOrder/ATF_TestOrder/ATF_TestOrder.py”   第18行,在       driver.switch_to_frame(“Uploadimage”)文件“C:\ Users \ Dell \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,   第775行,在switch_to_frame中       self._switch_to.frame(frame_reference)文件“C:\ Users \ Dell \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ switch_to.py”,   第89行,在框架中       self._driver.execute(Command.SWITCH_TO_FRAME,{'id':frame_reference})文件   “C:\用户\ DELL \应用程序数据\本地\程序\ Python的\ Python36-32 \ LIB \站点包\硒\ webdriver的\遥控\ webdriver.py”   第312行,执行中       self.error_handler.check_response(响应)文件“C:\ Users \ Dell \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py”,   第242行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchFrameException:消息:没有这样的   frame(会话信息:chrome = 65.0.3325.181)(驱动程序信息:   chromedriver = 2.37.544315   (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform = Windows NT   10.0.16299 x86_64)

1 个答案:

答案 0 :(得分:0)

我可能错了,但我认为你必须先找到iframe并切换到它

driver.switch_to_frame(driver.find_element_by_id('Uploadimage')) 

假设Uploadimage是一个id,你可能需要改变这个