0

I wanted to access the element with text as Deprecated within "http://demo.guru99.com/selenium/deprecated.html" link in selenium in chrome browser

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

I have used the above code but I am getting error. How do I do it?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sweta
  • 1
  • 1
    Please, clarify the error that you're getting to help others to answer your question. – Tiago Duque Sep 13 '19 at 16:56
  • You don't need switch twice.Just remove the last line `driver.switchTo().frame("classFrame");` – KunduK Sep 13 '19 at 17:08
  • Welcome to SO. Please take the time to read [ask]. It will help you craft solid questions that will hopefully get useful answers. What is the specific error? Edit your question directly to include it. – orde Sep 13 '19 at 18:28

1 Answers1

0

You were so close. Invoking frameToBeAvailableAndSwitchToIt() not only waits but also switches Selenium's focus within the <iframe>. So to click() on the element with text as Deprecated as the the desired elements are within a <frame> so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
  • Induce WebDriverWait for the desired elementToBeClickable.
  • You can use either of the following 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();
      

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks. I tried with the above solution but still I get the same error – Sweta Sep 17 '19 at 02:10
  • at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.frame(RemoteWebDriver.java:947) at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:503) at 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) – Sweta Sep 17 '19 at 02:12