首页 > 解决方案 > 如何将图片从电脑添加到网络

问题描述

我对简单的事情有疑问。

我无法将图像从我的电脑添加到网络。我在 Linux 上。

    driver = webdriver.Chrome(executable_path="/home/PycharmProjects/Drivers/chromedriver_linux64/chromedriver")
    driver.implicitly_wait(10)
    driver.get("my_web_page")
    
    driver.maximize_window()
    driver.find_element_by_xpath("//*[@id='root']/div[1]/div[2]/form/div[1]/input").send_keys("login")
    driver.find_element_by_xpath("//*[@id='root']/div[1]/div[2]/form/div[2]/input").send_keys("pwd")
    btn = driver.find_element_by_xpath("//*[@id='root']/div[1]/div[2]/form/button")
    btn.click()
    driver.find_element_by_xpath("//*[@id='root']/aside/section[2]/a[2]").click()
    driver.find_element_by_xpath("//*[@id='root']/main/div[2]/div[2]/div[1]/div[2]/a").click()

    driver.execute_script("window.scrollBy(0,925)", "")

    drp_list = driver.find_element_by_xpath("//*[@id='root']/main/div[3]/section[3]/div[2]/div[2]/div/div/div/button/label")
    drp_list.send_keys("/home/Desktop/ct.png")

运行脚本后我的控制台输出:

    selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

这是该元素在 html 中的外观:

input accept="image/*" type="file" autocomplete="off" tabindex="-1" style="display: none;">

标签: python-3.xlinuxseleniumautomated-tests

解决方案


ElementNotInteractableException

ElementNotInteractableException 是 W3C 异常,它被抛出以指示虽然元素存在于HTML DOM上,但它不处于可以交互的状态。

原因及解决方案:

ElementNotInteractableException发生的原因可能很多。

  1. 其他我们感兴趣WebElement的临时覆盖WebElement

在这种情况下,直接的解决方案是诱导ExplicitWaitieWebDriverWaitExpectedConditionas结合,invisibilityOfElementLocated如下所示:

    WebDriverWait wait2 = new WebDriverWait(driver, 10);
    wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
    driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();

更好的解决方案是获得更细粒度的,而不是ExpectedConditioninvisibilityOfElementLocated我们可以使用ExpectedCondition的那样使用elementToBeClickable,如下所示:

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
    element1.click();
  1. 其他我们感兴趣WebElement的永久覆盖WebElement

如果在这种情况下覆盖是永久覆盖,我们必须将WebDriver实例转换为JavascriptExecutor并执行单击操作,如下所示:

    WebElement ele = driver.findElement(By.xpath("element_xpath"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", ele);

推荐阅读