即使存在元素,驱动程序也无法在页面上找到元素 。基本上我只是尝试当一个弹出窗口(我们在许多网站上看到的广告弹出窗口)出现时,它应该被点击。以下是代码:
System.setProperty("webdriver.gecko.driver","C:\\SeleniumDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.get("https://www.sportskeeda.com/wwe");
//WebElement popup=driver.findElement(By.className("bullbg"));
WebElement popup=driver.findElement(By.xpath("//div[contains(text(),'Close')]"));
if (popup.isDisplayed())
{
System.out.println("True");
}
else
{
System.out.println("False");
}
错误
线程中的异常" main" org.openqa.selenium.NoSuchElementException: 无法找到element:// div [contains(text(),' Close')]
Please click here to check there is a popup which gets display
答案 0 :(得分:0)
首先尝试滚动到元素,某些东西可能会阻碍
WebElement element = driver.findElement(By.id("id"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
答案 1 :(得分:-1)
使用相反:
Boolean popup = driver.findElement(By.xpath(“// div [contains(text(),'Close')]”))。isDisplayed();
if(popup == true) {
System.out.println("True");
}
else
{
System.out.println("False");
}