【问题标题】:Not able to click on a link in a frame无法单击框架中的链接
【发布时间】:2020-01-20 22:19:18
【问题描述】:

我无法在运行模式下单击框架中的链接。首先,我切换到 Frame,然后单击一个链接。单击链接后,我想单击同一框架上的另一个链接。但无法点击第二个链接。在调试模式下,我可以单击这两个链接。

使用的网址:http://demo.guru99.com/selenium/deprecated.html

页面上有三个框架,我切换到名为“classFrame”的框架。在框架上,我单击名为“已弃用”的链接。我得到了“已弃用”链接的所有内容。现在我想点击我之前所在的“概述”链接。但我无法单击名为“概述”的链接。请帮我点击第二个链接“概述”。 我正在使用以下代码:

    System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
    WebDriver driverChrome = new ChromeDriver();
    WebDriverWait webWaitVar = new WebDriverWait(driverChrome, 1000);
    driverChrome.get("http://demo.guru99.com/selenium/deprecated.html");

    driverChrome.switchTo().frame("classFrame");

    driverChrome.findElement(By.linkText("Deprecated")).click();
    WebElement linkOverview = driverChrome.findElement(By.linkText("Overview"));
    webWaitVar.until(ExpectedConditions.visibilityOf(linkOverview));
    driverChrome.findElement(By.linkText("Overview")).click();

对于页面 HTML 代码,请参考链接,因为我无法将其粘贴到此处。非常感谢

【问题讨论】:

  • 你可能也想在 findElement 上使用等待。

标签: selenium xpath css-selectors frame webdriverwait


【解决方案1】:

click() 上的文本为 Deprecated 在 url http://demo.guru99.com/selenium/deprecated.html 的元素上,再次click() 上的文本为 Overview 的元素上,作为所需元素在 <frame> 内,因此您必须:

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

    • cssSelector:

      System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
      WebDriver driverChrome = new ChromeDriver();
      driverChrome.get("http://demo.guru99.com/selenium/deprecated.html");
      new WebDriverWait(driverChrome, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame[name='classFrame']")));
      new WebDriverWait(driverChrome, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.topNav a[href^='deprecated-list']"))).click();
      new WebDriverWait(driverChrome, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.topNav a[href^='overview-summary']"))).click();
      
    • xpath:

      System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
      WebDriver driverChrome = new ChromeDriver();
      driverChrome.get("http://demo.guru99.com/selenium/deprecated.html");
      new WebDriverWait(driverChrome, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[@name='classFrame']")));
      new WebDriverWait(driverChrome, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='topNav']//a[starts-with(@href, 'deprecated-list')]"))).click();
      new WebDriverWait(driverChrome, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='topNav']//a[starts-with(@href, 'overview-summary')]"))).click();
      

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

【讨论】:

  • 感谢您的回复。我可以先单击“已弃用”链接,但在“已弃用”之后无法单击“概述”链接。请以其他方式告诉我
  • @Achiver 查看更新的答案并让我知道状态。
  • 感谢 DebanjanB。我已将 elementToBeClickable 用于第二个链接“概述”并且它有效。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 2020-02-07
相关资源
最近更新 更多