单击 Selenium Javascript 中的元素无效

Click Element in Selenium Javascript not working

我正在自动化一个网站,我想在其中出现订阅弹出窗口时单击 x X 的 HTML 是

<button title="Close popup message" data-testid="email-popup-close-button" id="email-popup-close-button" class="dw-1hfj58t-CloseBtn--EmailContainer e1lwf35p0">×</button>

所以点击这个我使用下面的

await driver.findElement(By.id("email-popup-close-button")).click();

但出现以下错误

NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"*[id="email-popup-close-button"]"}

这似乎是定位器的问题。下面试试看。

await driver.findElement(By.xpath("(//button[normalize-space()='×'])[1]")).click();

await driver.findElement(By.xpath("//button[@title='Close popup message']")).click();

WebDriverWait wait = new WebDriverWait(driver,30);
elem = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@title='Close popup message']")));
elem.click();

导入:

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait

如果这不起作用,请告诉我。

找到答案 宣布这个 const {Builder, By, Key, until} = require("selenium-webdriver");

并使用

let query = driver.wait(until.elementLocated(By.id('email-popup-close-button')));
    await query.click();