python-3.x - 如何将图片从电脑添加到网络
问题描述
我对简单的事情有疑问。
我无法将图像从我的电脑添加到网络。我在 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;">
解决方案
ElementNotInteractableException
ElementNotInteractableException 是 W3C 异常,它被抛出以指示虽然元素存在于HTML DOM上,但它不处于可以交互的状态。
原因及解决方案:
ElementNotInteractableException发生的原因可能很多。
- 其他我们感兴趣
WebElement
的临时覆盖WebElement
:
在这种情况下,直接的解决方案是诱导ExplicitWait
ieWebDriverWait
与ExpectedCondition
as结合,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();
更好的解决方案是获得更细粒度的,而不是ExpectedCondition
像invisibilityOfElementLocated
我们可以使用ExpectedCondition
的那样使用elementToBeClickable
,如下所示:
WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();
- 其他我们感兴趣
WebElement
的永久覆盖WebElement
:
如果在这种情况下覆盖是永久覆盖,我们必须将WebDriver
实例转换为JavascriptExecutor
并执行单击操作,如下所示:
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
推荐阅读
- laravel - 我如何在登录中使用不同级别的令牌并使用 laravel 在所有功能中测试它
- r - R jupyter笔记本中具有聚集标准错误的回归表?
- azure-devops - 可以在 Azure Pipelines 中完成条件变量赋值吗?
- c - C中自发的缓冲区溢出
- json - 尝试解析 JSON 并创建提取的 JSON
- namespaces - Web Worker importScripts 无法将脚本变量放在全局范围内
- python - 无法从 myapp.models 导入名称
- database - 如何在我的颤振代码 sqlite 中解决这个问题
- sql - 仅当SELECT返回有效行时,仅在SELECT中插入表中
- java - 如何将用作 java main 方法输入的单引号解释为单引号,而不是将其解释为一个参数的开始和结束?