【问题标题】:How to wait for an item to finish loading?如何等待项目完成加载?
【发布时间】:2019-11-28 18:43:45
【问题描述】:

好吧,我正在使用 Selenium 使用 Angular 和 Java 设计的页面进行测试。当对数据库进行查询或页面在其代码中加载时,会出现:

<span _ngcontent-c0 class = "loading"> </span>

当它完成加载时,它会变成这样:

<span _ngcontent-c0 class = "loading" hidden> </span>

我的问题是这个“加载”正在拦截我在测试中发送的点击:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <a class="white-text" id="aaidEntidadBancaria" title="Apply"> ... </a> is not clickable at point (460, 502) . Other element would receive the click: <span _ngcontent-c0 = "" class = "loading"> </span>

我可以使用什么样的等待?我已经尝试过 invisibilityOfElementLocated(定位器),但它没有用......

【问题讨论】:

    标签: java selenium xpath css-selectors webdriverwait


    【解决方案1】:

    您需要等待 loader invisiblehiddeninvisibilityOfElementLocated() 引入 WebDriverWait并且您可以使用以下任一Locator Strategies

    • cssSelector:

      new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("span.loading)));
      
    • xpath:

      new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//span[@class='loading'])));
      

    您可以在Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working找到相关讨论


    一旦 loaderinvisiblehidden 您可以为 elementToBeClickable() 诱导 WebDriverWait 并调用 @ 987654330@如下:

    • cssSelector:

      new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("span.loading)));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.white-text#aaidEntidadBancaria[title='Apply']"))).click();
      
    • xpath:

      new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//span[@class='loading'])));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='white-text' and @id='aaidEntidadBancaria'][@title='Apply']"))).click();
      

    您可以在Element MyElement is not clickable at point (x, y)… Other element would receive the click找到相关讨论

    【讨论】:

      【解决方案2】:

      如果invisibility_of_element_located 不适合您,您可以尝试使用Javascript click 解决ClickIntercepted 问题:

      # locate clickable element
      clickable_element = WebDriverWait(driver, 10).until(
              EC.presence_of_element_located((By.XPATH, "//a[@title='Apply']")))
      
      # click with Javascript
      driver.execute_script("arguments[0].click();", clickable_element)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多