Visualforce SalesForce - iFrame issue - selenium

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?

Related

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

Karate Driver interaction with iframe

Having difficulty with Karate Driver and inputing data into fields that are in an iframe
Have tried using xpath and css selectors to the iframe so I could theoretically switch into and interact with the iframe without any luck. I can find the iframe but I am unsure how to switch context to the iframe so the scenario can continue inside the iframe. For example inputting values into fields in the iframe.
Help Please :)
Update:
Can successfully switch into an iframe but now running into an issue with nested iframe.
* switchFrame(0)
* click('.some-checkbox')
* switchFrame(0)
neither iframe has great css selectors. The second switchFrame with an index of 0 is not looking to the nested iframe.
I guess you have seen the docs here: https://github.com/intuit/karate/tree/develop/karate-core#switchFrame
I admit this is very tricky. Ideally you have a proper CSS or ID selector to the frame and this is an actual working example from a test I have. Note that the waitFor() may be what you are missing, especially when the <iframe> is some slow loading bloatware.
* waitFor('.some-css-name iframe').switchFrame()
* click('.some-checkbox')
* switchFrame(null)
And unfortunately I have found that this tends to work best on driver type: chrome and chromedriver

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

Selenium has trouble recognizing elements in nested iFrames

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));