.sendkeys方法无法使用Python Selenium上传文件

camagu4

我正在尝试自动化Facebook市场帖子。但是我正在努力上传图片。

我已经找到了元素。当我单击该元素时,它将显示“框”,其中显示了文件管理器,以便我可以单击文件夹,然后单击所需的图像。

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.ElementNotInteractableException:消息:元素不可交互

我也尝试使用os库:

ele.send_keys(os.getcwd() + '/home/br1/Downloads/rasp.jpeg')

获取相同的异常错误。

元素可见的html代码(代码中使用的元素):

<div class="_3jk">

是其父元素(元素不可见):

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

任何想法和建议将不胜感激。我是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")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章