【问题标题】:Selenium error: no such element: Unable to locate element on .isDisplayed() methodSelenium 错误:没有这样的元素:无法在 .isDisplayed() 方法上找到元素
【发布时间】:2020-02-27 04:18:56
【问题描述】:

我在下面有一个 if 语句,这给我带来了问题。如果在不同的下拉列表中进行了某些选择,页面将显示第二个下拉列表和一个复选框。下面的代码在做出导致这两个元素显示的选择时按预期工作,但如果做出不使它们显示的选择,则不会。我收到no such element: Unable to locate element 错误。起初我以为它会以任何一种方式返回,但问题是它崩溃了,因为。我什至在尝试将值分配给布尔值时添加了一个检查,但仍然得到相同的错误。

boolean dropdown = driver.findElement(By.id("DROPDOWN")).isDisplayed(); 得到同样的错误。

      if(driver.findElement(By.id("DROPDOWN")).isDisplayed()){

                driver.findElement(By.id("DROPDOWN")).click();
                driver.findElement(By.xpath("Choice in Drop DOWN)).click();
                driver.findElement(By.id("CheckBox")).click();  
     }

【问题讨论】:

  • webdriverwait 条件是什么样的?我有想过,但不知道如何解决
  • 我认为“.isDisplayed()”会检查样式属性,但如果元素不存在,它在调用方法之前不会抛出这样的元素。您应该使用带有 findElements 的 webdriverwait。如果不存在,它将返回一个空数组。

标签: java selenium selenium-webdriver


【解决方案1】:

findElement 方法将抛出此硬异常 - 如果未找到该元素,则没有此类元素。只需包含 No such Element 的异常处理,您的逻辑就可以正常工作。

try{
if(driver.findElement(By.id("DROPDOWN")).isDisplayed()){

                driver.findElement(By.id("DROPDOWN")).click();
                driver.findElement(By.xpath("Choice in Drop DOWN)).click();
                driver.findElement(By.id("CheckBox")).click();  
     }
catch (NoSuchElementException e)
{
// I believe you dont have to do anything here. May be a console log will do.
}

【讨论】:

  • 这确实有效,但似乎是一个奇怪的解决方案。我不明白该方法如何只返回 true 而不是返回 false,它会引发异常。该文件说它是一个布尔值。 selenium.dev/selenium/docs/api/java/org/openqa/selenium/…
  • findElement 方法在 isDisplayed 方法执行之前被执行。因此抛出错误。
【解决方案2】:

以下答案解释了如何处理检查元素是否存在并通过包装自定义方法来处理异常。

How to check if Element exists in c# Selenium drivers

我还建议您按照以下方式重写您的代码,以避免重复并避免使用 xpath 选择器。不需要在同一上下文中使用 findElement 两次,只需创建一个变量即可。

var dropdown = driver.findElement(By.id("DROPDOWN"));
if (dropdown.Displayed()) 
{
    var selectElement = new SelectElement(dropdown);
    selectElement.SelectByValue("valuehere");
}

如果您使用的是文本而不是选择框中的值,则可以使用 SelectByText("texthere");而不是 SelectByValue。

【讨论】:

  • 谢谢。链接中的解决方案与上面使用 try catch 的解决方案相同。我会尝试使用 xpath 以外的东西重写,但代码都是用 Angular 创建的,我们在定位元素时遇到了挑战。
  • 如果只是像示例中那样创建自己的 ElementIsVisible 函数,您可以调用它,而不是到处乱用 try catch
【解决方案3】:
如果元素存在于 DOM 中,

isDisplayed() 将起作用,后跟 style 属性:- display 不应为 false 或 none。

如果先前的操作是导致两个元素都显示的选择,则表示该元素在 DOM 中但不可见。所以检查可见性条件会返回错误。

尝试等待元素可见并对其执行检查操作,这将减少同步延迟。

WebDriverWait 等待 = new WebDriverWait(WebDriverRunner.getWebDriver(),5); wait.until(ExpectedConditions.visibilityOfElementLocated("按定位器"));

if (dropdown.isDisplayed()) 
   `````````// If the dropdown is tagged with <Select> tag
   ``````````` Select dropDown = new Select(dropdown);
    ```````````dropDown .selectByValue("value);

  ```````` // Else fetch the dropdown list item and store it in a list and iterate through and perform the desired action
    ```````````List<WebElement> dropDownList = new ArrayList<Webelements>;
    ```````````dropDownList.forEach(element -> {
        ```````````if(element.getText().equals("value")){
           ``````` ````element.click();
       ``````````` }
   ``````````` });

    ```````````driver.findElement(By.id("CheckBox")).click(); 
}    





【讨论】:

  • 在某些情况下,该元素将不可见。对于第一个选择,根据选择的内容,其他选项可用。
  • 因此,如果可能采取行动,第一个选择(Option1)将填充一些下拉列表(Option1-dd),第二个选择(Option2)将填充一些下拉列表(Option2-dd)...使用参数(WebElement 下拉列表,字符串值)创建一个通用私有方法,并使用定义的下拉定位器作为方法的输入从单个方法调用私有通用方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2021-07-07
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多