【问题标题】:I can't find the button via Selenium Java [closed]我无法通过 Selenium Java 找到按钮 [关闭]
【发布时间】:2020-12-31 05:27:11
【问题描述】:

我尝试通过 cssSelector 和 xPath 找到它,但仍然显示 NoSuchElementException

WebElement element = driver.findElement(By.cssSelector("button[class='btn btn-primary c-schedule-story-button ']"));
element.click();

Screenshot is here

【问题讨论】:

  • @tgdavies 这是一个有效的 CSS 选择器,他们只是不使用 .用于类并使用“类”作为属性链接到 Web 元素

标签: java selenium xpath automation nosuchelementexception


【解决方案1】:
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
WebElement element = driver.findElement(By.cssSelector("button..c-schedule-story-button"));
element.click();

它在一个 iframe 里面,你必须先切换到那个

如果你想再次与外部元素交互,你必须切换到默认内容

driver.switchTo().defaultContent()

【讨论】:

  • 无论如何测试都失败了。
  • 什么错误
  • 更新了你的班级看起来比我在网站上看到的错误的答案
  • org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素
  • 我更新了答案尝试新的定位器
猜你喜欢
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 2020-09-26
  • 2014-04-07
  • 2020-02-03
  • 1970-01-01
相关资源
最近更新 更多