0

I am challenged by this error using Wildcard(*) in a string to locate a frame. When I write the complete name of the iFrame in the statement, it has no issues finding the Frame.

driver.SwitchTo().Frame("*_ifr") 
OpenQA.Selenium.NoSuchFrameException: 'No frame element found with name or id *_ifr'

When I enter the entire frames name like below, it works just fine

driver.SwitchTo().Frame("txt-client-instructions_ifr") 

I expect the wildcard will find the following iframes:

  • 1st txt-client-instructions_ifr
  • 2nd 107314_100323_ifr
  • 3rd 107341_100324_ifr
  • 4th 100321_macrotext_ifr

Your thoughts are appreciated!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

SwitchTo().Frame() is the method to give you access to switch frames and windows. The argument type is of String. As an example:

driver.SwitchTo().Frame("FrameName");

String doesn't support wildcards where as By.CssSelector or By.XPath supports wildcards.

Hence,

driver.SwitchTo().Frame("txt-client-instructions_ifr")

is successful, where as:

driver.SwitchTo().Frame("*_ifr")

raises OpenQA.Selenium.NoSuchFrameException.


Update

As an alternative you can use either of the following locator strategies:

  • CssSelector:

    driver.SwitchTo().Frame(driver.FindElement(By.CssSelector("iframe[name$='_ifr']")));
    
  • XPath:

    driver.SwitchTo().Frame(driver.FindElement(By.XPath("//iframe[contains(@name, '_ifr')]")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks you for your great feedback something I did not know. I tried a different statement using xpath , but I still get a no element found. . is the following still wrong : driver.SwitchTo().Frame(driver.FindElement(By.XPath("//*[@id='*_ifr']"))); – user3393601 Feb 20 '23 at 18:35
  • @user3393601 Checkout the answer update and let me know the status. – undetected Selenium Feb 20 '23 at 20:10
  • Thanks again! it did work . The first statement below works perfect. May I ask why the 2nd statement that comes right after it is not working? I thought once you switch to the frame you want , you can then access any thing inside it so I used the second statement to add text in the text area but it does not work driver.SwitchTo().Frame(driver.FindElement(By.XPath("//iframe[contains(@id, '_ifr')]"))); driver.FindElement(By.XPath("//#document/html/body")).SendKeys("Your Text Here"); – user3393601 Feb 20 '23 at 21:16
  • Many thanks to you. I will – user3393601 Feb 20 '23 at 21:32