无此类元素错误:无此类要素:无法使用Selenium Javascript定位元素

wfveoks0  于 2023-01-30  发布在  Java
关注(0)|答案(1)|浏览(132)

我正在使用selenium和Javascript。当我想点击一个元素时,我得到这个错误。我使用了显式等待,但它没有帮助。有人能帮助我吗?
这是test.js文件:

const Browser = require("../Browser")
const MainPage = require("../pageobjects/MainPage")
const AlertPage = require("../pageobjects/AlertPage");
describe("Test 1", function(){
    it("test case",  async function(){
         await Browser.goToUrl("https://demoqa.com/")
         let mainPage = new MainPage();
         let isMainPageDisplayed = await mainPage.isMainPageDisplayed();
         expect(isMainPageDisplayed).to.be.true
         await mainPage.clickAlertFrameWindows()
         let alertPage = new AlertPage()
         let isAlertPageDisplayed = await alertPage.isAlertElDisplayed();
         expect(isAlertPageDisplayed).to.be.true
         await alertPage.clickAlertEl()
         await Browser.closeBrowser()
    })
})

AlertPage.js:

class AlertPage{
    #name = "Alert Page";
    #locator = "//span[text()='Alerts']"
    #alertEl = new BaseElement(this.#locator, "Alert button")

    clickAlertEl(){
 BrowserFactory.getBrowser().wait(until.elementLocated(By.xpath("//span[text()='Alerts']/ancestor::li[contains(@class,'btn')]")))
        this.#alertEl.clickWithExecutor()
 
    }
    isAlertElDisplayed(){
        // BrowserFactory.getBrowser().wait(until.elementLocated(By.xpath("//span[text()='Alerts']/ancestor::li[contains(@class,'btn')]")))
        return this.#alertEl.isElementPresent()
    }
}
module.exports = AlertPage;

HTML快照:

px9o7tmv

px9o7tmv1#

通常<span>标签更容易点击。因此在 * clickAlertEl() * 中尝试:

BrowserFactory.getBrowser().wait(until.elementLocated(By.xpath("//span[text()='Alerts']//ancestor::li[1]")))

或者干脆

BrowserFactory.getBrowser().wait(until.elementLocated(By.xpath("//span[text()='Alerts']")))

相关问题