1

I am having an issue with Selenium and not being able to switch to a frame. I have tried using the xpath of iframe but it replies that the element was not found. This was the code I was using:

driver.switch_to_frame(driver.find_element_by_xpath('//[@id="VF"]'))

I have also tried using a css-selector but it gives me the same result. I suspect that the div element that holds the iframe that I want to select is causing this issue but I'm not sure. Here's what that div element looks like:

<div id="dee_0" class="ar BAResults" fframe='<iframe title="SRC" src="javascript:&quot;&lt;HTML&gt;&lt;/HTML&gt;&quot;" onload="DVFol&#40;&#41;"></iframe>

Any help would be greatly appreciated.

edit this is the iframe I'm trying to target

<iframe name="VF" title="SRC" id="VF" src='javascript:"<HTML></HTML>"' frameborder="0" scrolling="auto" style="left: 0px; top: 0px; width: 830px; height: 490px; background-color: transparent;" allowtransparency="true" onload="F(0,302899400).ol()">

stack trace

Traceback (most recent call last):
File "C:/Users/kthao.ISC/PycharmProjects/AutoComplete/test2.py", line 32, in 
<module>
chrome.switch_to_frame(chrome.find_element_by_xpath('//*
[@id="VF"]'))
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 368, in 
find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 858, in find_element
'value': value})['value']
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\errorhandler.py", line 237, in 
check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: 
Unable to locate element: {"method":"xpath","selector":"//*
[@id="VF"]"}
3
  • Post the complete stack trace. Commented Dec 20, 2017 at 3:40
  • We are not seeing //[@id="VF"] anywhere in your html code Commented Dec 20, 2017 at 4:11
  • I just added the stack trace and the iframe I'm targeting. Commented Dec 20, 2017 at 17:39

1 Answer 1

2

The method to switch to an iframe is switch_to.frame() and the <iframe> tag contains the attribute title as SRC. So you can use the following line of code :

 driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@title='SRC']"))
Sign up to request clarification or add additional context in comments.

1 Comment

this did work but i had to use driver.switch_to_default_content() the only way i figure this out is to print all the code within the frame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.