【发布时间】:2022-01-23 19:49:34
【问题描述】:
【问题讨论】:
-
您应该尝试隔离要与之交互的元素。在您的代码中有多个元素具有 标签;因此,如果您使用该元素的特定路径会更好
-
请不要张贴文字图片,而是保留为文字。
标签: java selenium selenium-webdriver webdriver webdriverwait
【问题讨论】:
标签: java selenium selenium-webdriver webdriver webdriverwait
ElementNotInteractableException 是 W3C 异常,抛出该异常表示虽然 HTML DOM 上存在元素,但它不处于可以交互的状态。
ElementNotInteractableException 发生的原因有很多。
我们感兴趣的 WebElement 上的其他 WebElement 的临时覆盖:
在这种情况下,直接的解决方案是诱导 ExplicitWait 即 WebDriverWait 结合 ExpectedCondition 为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 strong> 为 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 并执行如下点击操作: p>
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
【讨论】:
ExpectedConditions吗?
我得到这个是因为我想与之交互的元素被另一个元素覆盖。在我的情况下,它是一个不透明的覆盖层,使所有内容都成为 r/o。
当试图点击另一个元素下的一个元素时,我们通常会得到“......其他元素会收到点击”,但并非总是如此:.(
【讨论】:
当元素不处于可交互状态时,我们会得到这个异常。所以我们可以使用等到元素被定位或变为可点击。
尝试使用隐式等待:
driver.manage().timeouts().implicitlyWait(Time, TimeUnit.SECONDS);
如果这不起作用,请使用显式等待:
WebDriverWait wait=new WebDriverWait(driver, 20);
WebElement input_userName;
input_userName = wait.until(ExpectedConditions.elementToBeClickable(By.tagName("input")));
input_userName.sendkeys("suryap");
您也可以使用ExpectedCondition.visibilityOfElementLocated()。
您可以增加时间,例如,
WebDriverWait wait=new WebDriverWait(driver, 90);
【讨论】:
Javascript 的解决方案如下所示。您必须根据需要修改时间。
driver.manage().setTimeouts({ implicit: 30000 });
希望这对某人有所帮助。 参考docs
【讨论】:
其实例外是Element Not Visible
最佳做法是在驱动程序实例化下方使用Implicit wait,以便有足够的时间在整个异常中查找元素
driver.get("http://www.testsite.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
仍然面临问题,因为某些元素需要更多时间。对单个元素使用ExplicitWait 以满足特定条件
在您的情况下,您面临元素not visible exception,然后按以下方式使用等待条件-
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_Elemetnt));
【讨论】:
在我的情况下,问题是因为有一些动画,并且该元素在一段时间内不可见。因此发生了异常。
由于某种原因,我无法使 ExpectedConditions.visibilityOfElementLocated 工作,因此我创建了一个代码等待硬编码几秒钟,然后再继续。
from selenium.webdriver.support.ui import WebDriverWait
def explicit_wait_predicate(abc):
return False
def explicit_wait(browser, timeout):
try:
Error = WebDriverWait(browser, timeout).until(explicit_wait_predicate)
except Exception as e:
None
现在我在任何我想等待的地方调用这个explicit_wait函数,例如
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Safari()
browser.get('http://localhost')
explicit_wait(browser,5) # This will wait for 5 secs
elem_un = browser.find_element(By.ID, 'userName')
【讨论】: