1

I have this could which should check if a iframe is available and swithes to it. Then I have this piece of code that check if a certain element is present.

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="stageFrame"]'))) 
try:
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="home_video_js"]')))
except NoSuchElementException:
    print("No video located")

I'm not sure why I still get the error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="stageFrame"]"}

Probably I'm missing something important but I don't know.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Russiahacker
  • 69
  • 1
  • 1
  • 8

1 Answers1

0

First of all the line of code for frame switching is out of the scope of the try-except{} block. So it is never handled.

Next, as you have used WebDriverWait on failure it should return TimeoutException

Finally, as you are switching to an iframe using the id attribute to be more canonical you may like to add the TAG_NAME as well i.e. iframe . So effective code block will be:

try:
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[@id="stageFrame"]'))) 
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="home_video_js"]')))
except TimeoutException:
    print("No iframe/video located")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • well heres the thing, the iframe i want to switch to is inside the iframe that i have already switched to. The stage ifram doesnt change but the second iframe changes depending on what type of lesson it is. How do i change to a iframe inside another iframe that i already switched too? – Russiahacker Apr 10 '22 at 21:18
  • I'm afraid, I don't get you. _`...the iframe i want to switch to is inside the iframe that i have already switched to...`_: we don't know about your first/second iframe. We only see your iframe switching attempt. You may like to raise a new question with all the details of your new requirement. – undetected Selenium Apr 10 '22 at 21:22
  • 1
    I know i make no sense but your suggestion worked ahahah. I havent used selenium in a while and im just confused too. Thanks again for the help – Russiahacker Apr 10 '22 at 21:39
  • @Russiahacker Glad to be able to help you. – undetected Selenium Apr 10 '22 at 21:42