1

I am using switching to an iframe using the below statements but on the final wait I get web element not found. Does anyone have any suggestions? I have trolled through google for about a day trying different techniques. None have worked so far

WebDriver driver = DriverFactory.getWebDriver()
WebDriverWait wait = new WebDriverWait(driver, 15)
driver.switchTo().defaultContent()
WebElement iframe = driver.findElement(By.cssSelector(".b-iframe__iframe"))
driver.switchTo().frame(iframe)
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(
 'fieldset.radio-switch-group')))

Screen shot of HTML below image HTML

Navarasu
  • 8,209
  • 2
  • 21
  • 32
Joe Batt
  • 11
  • 1
  • You get `NoSuchElementException` or `TimeoutException`? post the stack trace. – Guy Nov 05 '18 at 12:05
  • Stack trace: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: fieldset.radio-switch-group (tried for 15 second(s) with 500 MILLISECONDS interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:82) – Joe Batt Nov 05 '18 at 16:03
  • If i put in explicit wait and perform driver.findElement then I get this exception Stack trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"PaymentPage"}. Its ike the switch statement isnt working – Joe Batt Nov 05 '18 at 16:42
  • Can you share the code as copy past from your IDE? – Guy Nov 06 '18 at 06:40

1 Answers1

0

As per the HTML you have shared to locate the desired WebElement you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be visible and you can use either of the following solutions:

    • Using cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.b-iframe__iframe[src*='securetest']")));
      WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("fieldset.radio-switch-group#balance-type")));
      
    • Using xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='b-iframe__iframe' and contains(@src,'securetest')]")));
      WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//fieldset[@class='radio-switch-group' and @id='balance-type']")));
      

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks I've put that code in but with no luck. Its almost as if the switch isnt working. I have printed driver.pageSource and it is printing the entire DOM after the switch. I assumed that I should only get the iframe? – Joe Batt Nov 05 '18 at 16:09
  • @JoeBatt Check if the ` – undetected Selenium Nov 06 '18 at 05:12