无法在selenium python中上传图片

The Code i am using:

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")

Error:

C:\ Users \ Dell \ PycharmProjects \ ATF_TestOrder \ venv \ Scripts \ python.exe C:/Users/Dell/PycharmProjects/ATF_TestOrder/ATF_TestOrder/ATF_TestOrder.py Traceback(最近一次调用最后一次):文件“C:/ Users /在Dell.switch_to_frame(“Uploadimage”)文件“C:\ Users \ Dell \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib package \ 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:\ Users \ Dell \ AppData \ Local \程序\ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py“,第312行,执行self.error_handler.check_response(响应)文件”C:\ Users \ Dell \ AppData \ Local \程序\ Python的\ Python36-32 \ LIB \站点包\硒\的webdriver \ REM ote \ errorhandler.py“,第242行,在check_response中引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchFrameException:消息:没有这样的框架(会话信息:chrome = 65.0.3325.181)(驱动程序信息:chromedriver = 2.37.544315(730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform = Windows NT 10.0.16299 x86_64)

回答(1)

2 years ago

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

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

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