【问题标题】:How to swich to a frame and access element in it?如何切换到框架并访问其中的元素?
【发布时间】:2020-01-15 13:19:48
【问题描述】:

我想在 chrome 浏览器的 selenium 中的“http://demo.guru99.com/selenium/deprecated.html”链接中使用文本作为已弃用访问元素

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(driver.findElement(By.name("classFrame"))));
driver.switchTo().frame("classFrame");

我已经使用了上面的代码,但是我得到了错误。我该怎么做?

【问题讨论】:

  • 请澄清您遇到的错误,以帮助其他人回答您的问题。
  • 你不需要切换两次。只需删除最后一行driver.switchTo().frame("classFrame");
  • 欢迎来到 SO。请花时间阅读How to Ask。它将帮助您提出可靠的问题,这些问题有望得到有用的答案。具体错误是什么?直接编辑您的问题以包含它。

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

你离得很近。调用frameToBeAvailableAndSwitchToIt() 不仅会等待,还会在<iframe> 内切换Selenium 的焦点。所以要click() 在元素上的文本为 Deprecated 因为所需的元素在 <frame> 内,所以你必须:

  • 为所需的 frameToBeAvailableAndSwitchToIt 诱导 WebDriverWait
  • 为所需的 elementToBeClickable 诱导 WebDriverWait
  • 您可以使用以下任一Locator Strategies

    • cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame[name='classFrame']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.topNav a[href^='deprecated-list']"))).click()
      
    • xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[@name='classFrame']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='topNav']//a[starts-with(@href, 'deprecated-list')]"))).click();
      

在这里你可以找到Ways to deal with #document under iframe的相关讨论

【讨论】:

  • 谢谢。我尝试了上述解决方案,但仍然出现相同的错误
  • 在 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) 在 org .openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.frame(RemoteWebDriver.java:947) 在 org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:503) 在 org.openqa.selenium.support。 ui.ExpectedConditions$17.apply(ExpectedConditions.java:499) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208) at seleniumFirstCode.switchFrame.main(switchFrame.java:23)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-03
  • 1970-01-01
  • 2022-06-10
  • 2017-12-03
  • 1970-01-01
相关资源
最近更新 更多