为什么图像上传因selenium.common.exceptions.WebDriverException错误而失败:消息:未知错误:无法关注元素

时间:2018-06-11 09:20:49

标签: python selenium selenium-webdriver webdriver selenium-chromedriver

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 os
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/")
time.sleep(3)
driver.find_element_by_xpath('//*[@id="page_zazzleHeader_navSectionShop-title_7"]').click()
driver.find_element_by_xpath('//*[@id="Start_over"]').click()
driver.find_element_by_xpath('//*[@id="frame-canvas"]').click()
driver.find_element_by_xpath('//*[@id="computer"]').send_keys(os.getcwd()+"C://Users/Dell//Downloads//Living_Room.png")
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
  (Session info: chrome=66.0.3359.181)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.16299 x86_64)

1 个答案:

答案 0 :(得分:1)

此错误消息......

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element

...意味着 WebDriver 实例,即驱动程序无法专注于通过定位器策略识别的元素。

您的主要问题是,您尝试使用定位器策略在元素上调用send_keys()

driver.find_element_by_xpath('//*[@id="computer"]')

定位器策略标识 列表 元素,如下所示:

<li id="computer" style="width:160px !important;" class="computer firstUpload" data-api="" data-value="upload" title="Computer"></li>

<li>标记不接受任何字符序列。只有<input>代码接受字符序列。因此,当您尝试调用send_keys()时,驱动程序无法将焦点设置在该元素上。

因此图片上传失败。

元素快照:

li_element