Python 3.x .sendkeys方法无法使用Python Selenium上载文件
我正在尝试将facebook marketplace的帖子自动化。但我正在努力上传图片到它 我已经找到了元素。当我单击元素时,它将显示显示文件管理器的“框”,以便我可以单击文件夹,然后单击所需的图像Python 3.x .sendkeys方法无法使用Python Selenium上载文件,python-3.x,selenium,selenium-chromedriver,Python 3.x,Selenium,Selenium Chromedriver,我正在尝试将facebook marketplace的帖子自动化。但我正在努力上传图片到它 我已经找到了元素。当我单击元素时,它将显示显示文件管理器的“框”,以便我可以单击文件夹,然后单击所需的图像 ele = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="rc.js_c"]/div/div[1]/div[5]/div[2]/div/div/div/div/div[1]/div/div/span/div/a/div[2]'))
ele = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="rc.js_c"]/div/div[1]/div[5]/div[2]/div/div/div/div/div[1]/div/div/span/div/a/div[2]')))
ele.click()
但当我尝试这个:
ele.send_keys('/file_path/rasp.jpeg')
它引发了这一例外:
selenium.common.exceptions.ElementNotInteractiableException:消息:元素不可交互
我还尝试使用操作系统库:
ele.send_keys(os.getcwd() + '/home/br1/Downloads/rasp.jpeg')
获取相同的异常错误
元素可见的html代码(代码中使用的元素):
任何想法和建议都会被采纳。
我是Linux用户。您应该尝试使用输入发送文件路径,而不是div 试试下面的方法
ele = wait.until(EC.presence_of_element_located((By.XPATH,'//input[@name="composer_photo" and @type="file"]')))
ele.send_keys("file_to_be_uploaded")
您需要将密钥发送到输入标记。(它会将“value”属性设置为您的路径…)这与div上传照片有相同的问题吗?它在不使用操作系统模块的情况下工作。非常感谢你。
<input accept="image/*" multiple="" name="composer_photo" title="Elige un archivo para subir" data-testid="add-more-photos" display="inline-block" type="file" class="_n _5f0v" id="js_wg">
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By 10
# driver protocols
options = Options()
options.add_argument('disable-notifications')
options.add_argument('start-maximized')
driver = webdriver.Chrome(options=options, executable_path='/chromedriver')
wait = WebDriverWait(driver,10)
# url
driver.get('http://facebook.com/marketplace')
driver.implicitly_wait(10)
# logging
driver.find_element_by_id('email').send_keys('username')
driver.find_element_by_id('pass').send_keys('password')
driver.find_element_by_id('u_0_2').click()
# entering marketplace
driver.find_element_by_xpath('//*[contains(text(), "Vender algo")]').click()
driver.find_element_by_xpath('//*[contains(text(), "Artículo en venta")]').click()
ele = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="rc.js_c"]/div/div[1]/div[5]/div[2]/div/div/div/div/div[1]/div/div/span/div/a/div[2]')))
ele.send_keys('/file_path/rasp.jpeg')
ele = wait.until(EC.presence_of_element_located((By.XPATH,'//input[@name="composer_photo" and @type="file"]')))
ele.send_keys("file_to_be_uploaded")