0

I have been trying to understand how switching to a frameset works using py-selenium with no avail.

The website I am stating here is very similar to the web application that I am working on. https://www.quackit.com/html/tutorial/frame_example_frameset_1.html

Ideally I would like to access element-1 in this image and then move to the second frame and access element-2.

image

Bill Bell
  • 21,021
  • 5
  • 43
  • 58
Chan8211
  • 1
  • 1
  • 1
  • 1
    Possible duplicate of [selecting an iframe using python selenium](https://stackoverflow.com/questions/7534622/selecting-an-iframe-using-python-selenium) – DJK Aug 09 '17 at 16:42
  • Thank you responding Bill Bell. However I am looking at a frameset and not iframe. I am able to move across iframes which python selenium identifies the element as frames. But the tag frameset nor the frame src is identifed as frames. – Chan8211 Aug 09 '17 at 16:50
  • That wasn't me. :) I looked at the HTML and could see that it includes a couple of `frame` elements. That's why I felt justified in altering the text in your question slightly. – Bill Bell Aug 09 '17 at 17:03
  • Can you describe your issue in more details? Also share the code you've tried along with the exception log (if you get any). Note that you don't need to *switch to frameset* as `frameset` is common WebElement and should be handled appropriatelly – Andersson Aug 09 '17 at 17:08
  • I am sorry Bill Bell. My bad. This is my first ever question here. And well, I was able to resolve this with some mystery. Previously when I had tried to use find_element_by_xpath to dig the frame, I was not able to, but after a couple of coffees and 2 hours, I was able to get it. :-) – Chan8211 Aug 09 '17 at 17:55
  • Thank you Anderson but this worked !! driver.get("https://www.quackit.com/html/tutorial/frame_example_frameset_1.html") frame1=driver.find_element_by_xpath('/html/frameset/frame[1]') driver.switch_to_frame(frame1) – Chan8211 Aug 09 '17 at 17:55
  • Seems you got an answer to your Question. Let me know if you are still looking out for a better option. – undetected Selenium Aug 10 '17 at 04:04
  • @Chan8211, yep this how switching to **frame** works... But in your question you ask about how to switch to **frameset**. Next time be more explicit about what exactly you want your code to do – Andersson Aug 10 '17 at 09:22
  • I need a target frame of the form: `html/frameset/frameset[1]/frame[2]`, the idea of ignoring `frameset` doesn't make sense to me. The first `frameset` does not even contain any `frames`, just other `framesets`. Ignoring the `framesets` would be a hierarchical mistake. Just using the frame name doesn't work either. Please help! – Yan King Yin Mar 17 '21 at 17:35

1 Answers1

1

Here is one approach.

Load the initial page. Use an xpath expression to find the two frame elements. The, for each of them, get its url. Now you can use driver.get again (for each url) to load the page corresponding to the frame, and then find the p element that you want.

>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get('https://www.quackit.com/html/tutorial/frame_example_frameset_1.html')
>>> for frame in driver.find_elements_by_xpath('.//frame'):
...     frame.get_attribute('src')
...     
'https://www.quackit.com/html/tutorial/frame_example_left.html'
'https://www.quackit.com/html/tutorial/frame_example_right.html'

Any questions, please ask. If this does what you want, please mark the answer 'accepted' since that's the protocol on SO.

Bill Bell
  • 21,021
  • 5
  • 43
  • 58
  • Hey Bill, this worked !!! driver.get("quackit.com/html/tutorial/frame_example_frameset‌​_1.html") frame1=driver.find_element_by_xpath('/html/frameset/frame[1]‌​') driver.switch_to_frame(frame1) – Chan8211 Aug 09 '17 at 17:58
  • Yup, that was a possibility. I was looking for an answer as simple an answer as possible. – Bill Bell Aug 09 '17 at 18:01