代码调用不相关的 Web 元素

Code is invoking an irrelevant web element

提问人:kunal kaklij 提问时间:7/7/2023 更新时间:7/7/2023 访问量:11

问:

这是我正在执行的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="PROJECT.Suite">
  <test name="PROJECT.Test">
    <classes>
      <class name="testCases.ClickPrefPanel"/>
      <class name="testCases.Company_ExpandDepth_select"/>
      <class name="testCases.ClickOrgPanel"/>
      <class name="testCases.ClickReloadOrgBtn"/>
      <class name="testCases.ClickDivisionLevel"/>
    </classes>
  </test> <!-- PROJECT.Test -->
</suite> <!-- PROJECT.Suite -->

我的代码在这一行失败:<class name="testCases.ClickReloadOrgBtn"/>

这是 ClickReloadOrgBtn 类的代码:

package testCases;

import org.testng.annotations.Test;

import pageObjects.OrgTreeObjects1;
import testBase.BaseClass;

public class ClickReloadOrgBtn extends BaseClass{
    
    @Test
    public void clickReloadBtn() {
        OrgTreeObjects1 org = new OrgTreeObjects1(driver);
        org.reloadBtn();
    }

}

根据代码;代码必须进入 OrgTreeObjects1 类,然后单击 Reload Button。但相反,它初始化了这个 WebElement PracticeArea 并给了我 NoSuchElementException。仅附加 OrgTreeObjects1 类的相关代码:

package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class OrgTreeObjects1 extends BasePage{

    public OrgTreeObjects1(WebDriver driver) {
        super(driver);
    }
    
    /******************************************************************* Locators ******************************************************************/
    
    @FindBy(xpath = "//div[@id='westPanel']//div[@id='westPanel-body']//a[@data-qtip='Reload']//*[contains(@class,'x-btn-icon-el')]")
    WebElement ReloadOrgBtn;
    
    @FindBy(xpath = "//div[@id='OrgTree-bodyWrap']/div[@id='OrgTree-body']/div[@aria-valuetext='Loading...']//div[text()='Loading...']"
            + "//ancestor::div[@aria-valuetext='Loading...']//preceding-sibling::div[contains(@class,'x-masked') and @aria-busy='true']") // WebElement of Loading Element in Org Tree after clicking Reload button
    WebElement OrgTreeLoadingBox;
    
    String division = "//*[@class='x-grid-item-container']//span[normalize-space()='"+bundle.getString("Division")+"']";
    WebElement Division = driver.findElement(By.xpath(division));
    WebElement ExpandDivision = driver.findElement(By.xpath(division+"//parent::div//div[contains(@class,'expander')]"));
    
    String practiceArea = "//span[normalize-space()='"+bundle.getString("PracticeArea")+"']";
    WebElement PracticeArea = driver.findElement(By.xpath(practiceArea+"//ancestor::table//following-sibling::table//span[normalize-space()='"+bundle.getString("PracticeArea")+"']"));
    
    /******************************************************************* Actions ******************************************************************/
    
    public void reloadBtn() {
        w2.until(ExpectedConditions.elementToBeClickable(ReloadOrgBtn)).click();
        log.info("Reload button on Org Tree is clicked");
        if(w2.until(ExpectedConditions.visibilityOf(OrgTreeLoadingBox)).isDisplayed()) {
            w5.until(ExpectedConditions.invisibilityOf(OrgTreeLoadingBox));
        }
    }
}

我尝试注释 WebElement PracticeArea 行,然后代码工作正常。考虑到相同的语法,我预计我可能会对 WebElement Division 产生相同的错误。但我没有收到错误。我不明白为什么。

我知道当我们创建对象的实例时,其成员会被初始化。但是,为什么 Division WebElement 没有初始化呢?我不明白。两天来,我一直被困在这个问题上。

我尝试将 Web 元素放在一个方法中,然后从该方法返回 Web 元素并将其分配给 Web 元素。但它的效果并不好。附加代码:

WebElement PracticeArea = getPracticeArea(driver);
private WebElement getPracticeArea(WebDriver driver) {
        return driver.findElement(By.xpath(PracticeArea+"//ancestor::table//following-sibling::table//span[normalize-space()='"+bundle.getString("PracticeArea")+"']"));
    }
Java Object OOP 方法 构造函数

评论


答: 暂无答案