Selenium has trouble recognizing elements in nested iFrames - selenium

I am using Selenium Server to test a widget-based webpage. All of the widgets are contained within an iFrame, and each widget contains its own iFrame. I'm trying to manipulate elements within specific widgets specified by title, but Selenium seems to be unable to recognize anything past the second iFrame.
The title of the widget is within the first iFrame, and I need to use it to determine which iFrame to select next. The code that I'm using to select the frame looks something like this:
selenium.selectFrame("css=div.widgetheader:contains(TITLE)+widgetbody iFrame);
However, when I attempt to access the elements within the iFrame, Selenium is unable to locate them.
Any thoughts on how to get Selenium to recognize the elements within this second frame?

If you try to enter iframe that is nested inside another iframe, 2 iframe "switching" must be perform.
For example:
driver.switchTo().frame(driver.findElement(By.id("frameId1"))); //switching driver to first iframe
driver.switchTo().frame(driver.findElement(By.id("frameId2"))); //switching driver to second iframe
//Now, do your stuff.
To return to initial location (pare, use:
driver.switchTo().defaultContent();
Also, there can be situations when we not be able to get the iframe values. In this case, you can use tagName method.
driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));

Related

Visualforce SalesForce - iFrame issue

I am developing some selenium tests for salesforce website with webdriver (Selenium & Java) and I am facing a problem switching to different iframes.
The problem is that I want to select a DOM element but I can’t because its located inside an iframe, so in order to select it I have to switch to the correct iframe.
I can find all the iframes of the pages but if those iframes include other iframes (as children) it produces a big problem in switching from parent to children and searching for a specific DOM element inside those frames.
Any solution.
Switch to iframe, wait for elements, nothing works. any solution?

Getting "Nosuch element Exception" in Selenium Though XPATH is correct. Not sure is this due to Shadow DOM. Please confirm

I am trying to automate Salesforce application Using Selenium and getting NoSuchelementException though XPATH is correct and valid for particular object. When i have searched the issue, it might be reason for Shadow DOM.
For EX:
So XAPTH i have written like,
driver.findElement(By.xpath("//input[#name='Name']")).sendKeys("Jams");
or
driver.findElement(By.xpath("//input[#id='input-299']")).sendKeys("Jams");
This XPATH is highlighting in Console as well. But while automating it throws nosuchelement error.
So while checking for ShadowDOM option, am getting option like this for Name Object.
#shadow-root(user-agent)
Shadowroot DIV
-- nothing mentioned in div. it just open and closed tags.
How to automate this?
You can check if there are any iframes in your Dom. Simply do //iframe in your page developer mode(F12)> elements tab > search (Ctrf+F) area. If there are any, you will get the number of iframes.
Now if your textbox is in any of iframe use below code to go inside particular iframe first
driver.switch_to.frame("<name or Id of frame>")
then come out to frame use below:
driver.switch_to.parent_frame()
Also, if the issue is not related to frames check below for shadow-root related issue:
you can check below for shadow-root element ( Question is for Java, but you can co-relate):
How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector
The website contents are mostly created now using javascript. You may have to wait for certain elements to load before doing anything to it.
https://seleniumbyexamples.github.io/wait

Get the xpath of an element inside an iFrame for RobotFramework RIDE

I am trying to get the xpath of a 'Form' element by its id that is inside an iframe.
In chrome xpath plugin when i query
//iframe[contains(#id,'fraModalPopup')]
it gets me the iframe but when i try to get anything down the hierarchy it just returns null. e.g. if i try doing
//iframe[contains(#id,'fraModalPopup')]/html // returns null
or
//iframe[contains(#id,'fraModalPopup')]/form[contains(#id='aspnetForm')]
// not sure if it is a right xpath statement - also returns null
would please anyone guide me how I can get hold on to the form element? I have to use this xpath inside RIDE (Robot Framework).
iframe is an element inside main HTML DOM that contains its own embedded HTML DOM. You don't need to use iframe as context node to find form inside frame, but you need to switch to that iframe
select frame id=fraModalPopup
to be able to handle elements inside embedded HTML DOM (no need to add "//iframe" to XPath)
xpath=//form[#id='aspnetForm']

Scraping iframe using Selenium

I want to scrape ads in websites but many of them are dynamic and they are DOM objects. For example in
this snippet
I can get the iframe tag by Selenium but I cannot go any further. I think it is because of the XPATH. In this case the XPATH of the <html> inside the iframe is /html which is the same as the main page <html>.
This is the line of code that use:
element = WebDriverWait(self.driver,20).until(EC.presence_of_all_elements_located((By.XPATH, '/html')))
Any suggestions?
By default the selenium.webdriver object is set to the default page which it has parsed. To get the iframe data you will have to switch to the given iframe.
driver = webdriver.Chrome(executable_path=path_chrome)
# find the frame using id, title etc.
frame = driver.find_elements_by_xpath("//iframe[#title='iframe_to_get']")
# switch the webdriver object to the iframe.
driver.switch_to.frame(frame[i])
Always remember, if iterating over the iframes then to SWITCH BACK to the default webpage. Otherwise you won't be able to switch to other iframes in same code.
driver.switch_to.default_content()
Update
Below mentioned functions are deprecated now. So i have updated the answer.
driver.switch_to_frame('Any frame') #deprecated
driver.switch_to_default_content() #deprecated
To switch into an iframe on a page, you should use
driver.switch_to.frame:
iframeElement = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(iframeElement)
You can now use the driver to find elements within the iframe.
To switch back out of the iframe, use driver.switch_to_default_content()

How to work with iframe which is part of a webpage but not getting identified through webdriver?

I am trying to automate a webpage using webdriver,here i am struck with a iframe,which i dont know how to handle.
While i choose css for the iframe by selecting with ,it gives me #xEditingArea
again if I search the same iframe using the css or id,it is not identifying anything.
I tried everything
I want to write some message with the message body which is iframe.
Can anyone guide me how to handle this?
Thanks in advance.
If it is only one iFrame on your website you could try to access it with XPath and the tagname.
Directly accessing iframes is not possible, it has its own DOM elements and we have to switch to that particular frame and then perform the actions you want.
To select the iframe you want to work with, do:
driver.switchTo().frame("frame1");
Now, your driver set to work with the DOM the iframe one.
It's important to remember that maybe switching "back" will be needed. It's done like this:
driver.switchTo().defaultContent();
Using Python Selenium WebDriver, you can access a frame using:
from selenium import webdriver
browser = webdriver.Firefox()
browser.switch_to_frame("fame_name_or_id")
If you want to confirm that the frame was access correctly, just print out the page contents using:
print browser.page_source