Unable to read element in a iframe

409 views
Skip to first unread message

Nancy

unread,
Mar 15, 2018, 2:10:06 PM3/15/18
to Selenium Users
Hello team,

I am learning selenium and we are using selenium python bindings  and i need some input on this step :

i am trying to read SubFrame element by ID value  ...Switch to SubFrame  which seems to be working but i am unable to read title tag and verify if it contains 'React APP ' value  .  Here below is snippet of code and HTML sample is attached . Any help is appreciated 


try: 
        Getframe = driver.find_element(By.ID,'SubFrame')
        print("TestPass : Subframe found") 

except Exception as e:
        print("exception found", format(e))         
    
    #  Looks like i can read name attribute as  SubFrame   in this step                                                          
    print("Frame title " + Getframe.get_attribute("name"))
    driver._switch_to.frame('SubFrame')  
    # after this step i need to get the title tag and check if the value contains React App but not sure how to acheive this as i can't read any elements on the subframe to perform other steps
   

screenshot_html.jpg

Nancy

unread,
Mar 16, 2018, 11:26:02 AM3/16/18
to Selenium Users
Any suggestions . I am currently blocked so any help would be appreciated

Travis

unread,
Mar 16, 2018, 4:11:18 PM3/16/18
to Selenium Users
I don't know python, based on what I see here, it looks like you want this:

driver.switch_to_frame('SubFrame')  

Note that I removed an underscore ("driver._switch_to") changed a . to _ ("to_frame")

If that's not the problem (again, I don't know python, so I could be missing something), are there any errors found if you put the driver._switch_to.frame line in a try block?  

Assuming that is in fact working as expected, can you share the code you've tried for getting the title text after the frame switch?

Travis

R Morgan

unread,
Mar 16, 2018, 4:30:31 PM3/16/18
to seleniu...@googlegroups.com
Give this a try: 

driver.switch_to.frame(driver.find_element_by_name(name))
to get back to the main window do:
driver.switch_to.default_content()
Hope that helps.



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/8de589ad-48ad-4a0c-b08c-3276e25ba379%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
If you ask me if it can be done. The answer is YES, it can always be done. The correct questions however are... What will it cost, and how long will it take?

Nancy

unread,
Mar 17, 2018, 7:24:21 PM3/17/18
to Selenium Users
This is below error i get :



E       selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression .//[contains(@title,'React App')] because of the following error:
E       SyntaxError: Failed to execute 'evaluate' on 'Document': The string './/[contains(@title,'React App')]' is not a valid XPath expression.
E         (Session info: chrome=65.0.3325.162)
E         (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64)

testemv\lib\site-packages\selenium\webdriver\remote\errorhandler.py

Here below is sample snippet  code i am running 

 try: 
        Getframe = driver.find_element(By.ID,'SubFrame')
        print("TestPass : Subframe found") 
         
    except Exception as e:
        print("exception found", format(e))      
    
      
    #  Switch to SubFrame                                                            
    print("Frametitle " + Getframe.get_attribute("name"))
    ## Give time for iframe to load ##
    driver.implicitly_wait(3)    
    driver.switch_to.frame(driver.find_element_by_name('SubFrame'))
    driver.find_element_by_tag_name("title")
    driver.find_element_by_xpath(".//[contains(@title,'React App')]")
    
    if(Getframe == "null"):
        print("frame is empty")        
    else:
        print("frame exists, frame loaded")         







On Thursday, March 15, 2018 at 2:10:06 PM UTC-4, Nancy wrote:

R Morgan

unread,
Mar 18, 2018, 4:11:02 AM3/18/18
to seleniu...@googlegroups.com
Your xpath for the title is incorrect. But we would need to see the html to know why...

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Travis

unread,
Mar 18, 2018, 1:17:00 PM3/18/18
to Selenium Users
The XPath is missing a tag name. Should be either this:
.//title[@text='text here']
Or this
.//*[@text='text here']

Just be sure to replace "text here" with the text you're looking for (I'm on mobile and it's not letting me see your original post while I type a reply).

Nancy

unread,
Mar 18, 2018, 5:12:25 PM3/18/18
to Selenium Users

I have tried changing Xpath as per your recommendations  in both ways as below but still ...it appears it is missing 


1) driver.find_element(By.XPATH, ".//title[@text='React App']")
2) driver.find_element(By.XPATH, ".//*[@text='React App']")


Here below is the response output :

1) E       selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//title[@text='React App']"}
E         (Session info: chrome=65.0.3325.162)
E         (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64)

testemv\lib\site-packages\selenium\webdriver\remote\errorhandler.py:242: NoSuchElementException

2) E       selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@text='React App']"}
E         (Session info: chrome=65.0.3325.162)
E         (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64)

testemv\lib\site-packages\selenium\webdriver\remote\errorhandler.py:242: NoSuchElementException




On Thursday, March 15, 2018 at 2:10:06 PM UTC-4, Nancy wrote:

Travis

unread,
Mar 18, 2018, 6:19:21 PM3/18/18
to Selenium Users
OK, your xpath is valid now. NoSuchElementException means selenium can't find the element, either because it doesn't exist, or because of some other issue (if you're in the wrong iframe, for example).

One thing to try is the xpath without the . before the slashes.

It also couldn't hurt to test the XPath in your browser dev tools. If you're using Chrome or Firefox in windows, f12 will open the dev tools window. Select the html tab, then ctrl-f and paste in your xpath. If it's correct, it will find your title element. (that last paragraph is all from memory, so apologies if I missed any details).

Nirmala Kothapalli

unread,
Mar 21, 2018, 9:47:44 PM3/21/18
to seleniu...@googlegroups.com
Looks like when i put this  "/html/head/title[1]" i am able to read the subframe but how to get the value in the title tag.
I said gettitle but it says it has no attribute


driver.find_element(By.XPATH, "/html/head/title[1]")
driver.gettitle()
E       AttributeError: 'WebDriver' object has no attribute 'gettitle'

test_BulkParameterUI.py:77: AttributeError


--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/TTnYyJ6ijLE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-users+unsubscribe@googlegroups.com.

To post to this group, send email to selenium-users@googlegroups.com.

Magesh N

unread,
Mar 22, 2018, 12:20:56 AM3/22/18
to Selenium Users
We would like to see the html to figure out what is wrong?


On Thursday, March 22, 2018 at 7:17:44 AM UTC+5:30, Nancy wrote:
Looks like when i put this  "/html/head/title[1]" i am able to read the subframe but how to get the value in the title tag.
I said gettitle but it says it has no attribute


driver.find_element(By.XPATH, "/html/head/title[1]")
driver.gettitle()
E       AttributeError: 'WebDriver' object has no attribute 'gettitle'

test_BulkParameterUI.py:77: AttributeError
On Sun, Mar 18, 2018 at 6:19 PM, Travis <travis...@gmail.com> wrote:
OK, your xpath is valid now. NoSuchElementException means selenium can't find the element, either because it doesn't exist, or because of some other issue (if you're in the wrong iframe, for example).

One thing to try is the xpath without the . before the slashes.

It also couldn't hurt to test the XPath in your browser dev tools. If you're using Chrome or Firefox in windows, f12 will open the dev tools window. Select the html tab, then ctrl-f and paste in your xpath. If it's correct, it will find your title element. (that last paragraph is all from memory, so apologies if I missed any details).

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/TTnYyJ6ijLE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages