You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium开发Facebook Marketplace销售帖发布程序时遭遇ElementNotInteractableException的图片上传问题求助

Fixing ElementNotInteractableException for Facebook Marketplace Image Upload with Selenium

Hey there! I’ve run into similar headaches with Facebook’s tricky dynamic UI elements before—let’s break down why this is happening and how to fix it.

Why Your Current Code Isn’t Working

The element you’re targeting (<i class='hu5pjgll lzf7d6o1'>) is just the visual upload icon, not the actual file input field that handles file submissions. Google Images likely exposes the direct <input type="file"> element, which is why your same approach works there. Facebook hides this input element behind their custom UI, so Selenium can’t interact with the icon directly to send a file path.

Step-by-Step Solution

  1. Locate the hidden <input type="file"> element tied to the upload button (this is the only element that accepts file paths via SendKeys).
  2. Replace Thread.Sleep with WebDriverWait (way more reliable for dynamic pages like Facebook—no more guessing wait times).
  3. Send the file path directly to the input element (even if it’s hidden, Selenium allows this specifically for file inputs).

Modified Code Example

Here’s how to adjust your button1_Click method:

private void button1_Click(object sender, EventArgs e)
{
    // Keep your existing login/cookie handling code
    driver.FindElement(By.XPath("//button[normalize-space()='Akceptuj wszystkie']")).Click();
    driver.FindElement(By.XPath("//input[@id='email']")).SendKeys("login");
    driver.FindElement(By.XPath("//input[@id='pass']")).SendKeys("password");
    driver.FindElement(By.XPath("//button[contains(text(),'Zaloguj się')]")).Click();

    // Use WebDriverWait for reliable element waiting
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    
    // Navigate to create a listing
    wait.Until(d => d.FindElement(By.XPath("//*[name()='path' and contains(@d,'M17.5 23.7')]"))).Click();
    wait.Until(d => d.FindElement(By.XPath("//div[@class='j83agx80 cbu4d94t d6urw2fd dp1hu0rb l9j0dhe7 du4w35lb']//span[@class='d2edcug0 hpfvmrgz qv66sw1b c1et5uql lr9zc1uh a8c37x1j keod5gw0 nxhoafnm aigsh9s9 d3f4x2em fe6kdd0r mau55g9w c8b282yb iv3no6db jq4qci2q a3bd9o3v lrazzd5p knomaqxo']//span[1]"))).Click();
    wait.Until(d => d.FindElement(By.XPath("//span[contains(text(),'Przedmiot na sprzedaż')]"))).Click();

    // Find the hidden file input (adjust XPath if needed)
    IWebElement fileInput = wait.Until(d => d.FindElement(By.XPath("//input[@type='file']")));
    
    // Optional: If the input is still un-interactable, force it visible with JS
    // ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.display = 'block';", fileInput);

    // Send the file path directly to the input
    fileInput.SendKeys(@"C:\download\maciek.jpg");
}

Quick Tips

  • Tweak the file input XPath: Facebook uses dynamic class names, so if the basic //input[@type='file'] doesn’t work, inspect the upload area in your browser’s dev tools to find the exact input element (it’s usually nested inside the upload button’s container).
  • Watch for iframes: If the upload section is inside an iframe, you’ll need to switch to that iframe first with driver.SwitchTo().Frame(iframeElement) before locating the input.
  • Ditch Thread.Sleep: It’s brittle and can break if Facebook’s page loads slower than expected. WebDriverWait waits until elements are ready to interact with.

Let me know if you need help refining the XPath for the file input—just right-click the upload button in dev tools and check its parent elements for the hidden <input type="file">!

内容的提问来源于stack exchange,提问作者thomas2075

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:37:34