Switch frame if iframe has same name and id in Selenium [duplicate] - selenium

This question already has answers here:
Is it possible to switch to an element in a frame without using driver.switchTo().frame("frameName") in Selenium Webdriver Java?
(2 answers)
Closed 3 years ago.
I have frame where id and name is same and I want to switch the frame. How can I do this?
This what I tried:
driver.switchTo().frame("iframe");
driver.switchTo().frame("clntcap_frame");
driver.switchTo().frame("//iframe[#id='clntcap_frame']");
HTML code
<iframe name="clntcap_frame" id="clntcap_frame" style="" xpath="1"></iframe>
In console:
Exception in thread "main" org.openqa.selenium.NoSuchFrameException: No frame element found by name or id iframe

First, make sure that the iframe is present in the DOM. If it is present then followings should work l.
Try this:
In this method, you try to pass the frame element itself.
//To check the element exists or not but avoiding any exception, find all the elements with the id and put them in a list. Since id is a unique identifier, it should return only one.
List<WebElement> frame = driver.findElements(By.id("clntcap_frame"));
//Then check if the list of elements is empty or not. If not, then the frame is present in the DOM
if(!frame.isEmpty()) {
//Since frame is a list we need to get the element in the index 0
driver.switchTo().frame(frame.get(0));
} else {
System.out.println("Frame with this id doesn't exists");
}

Related

How to locate an element that doesn't have element name using xpath? [duplicate]

This question already has answers here:
How can I make generated content selectable?
(2 answers)
Closed 3 years ago.
Hi can anyone let me know if it is possible to locale an element that doesn't have element name using xpath? The element I want to click is a 'x' button beside a label, and it doesn't have a name, I find it is "::after" when I inspect element in browser.
::after
You get element using JavaScript. Example how to get content from pseudo element.
element = wait.until(ec.visibility_of_element_located((By.CSS_SELECTOR, "span.business-in-area")))
element_content = driver.execute_script("return window.getComputedStyle(arguments[0], ':before').getPropertyValue('content');", element)

how to switch to a frame set within a frame in selenium web driver

I want to access elements under frame xxx_info_new . This is the code flow and i get no such frame exception when i try to access xxx_info_new frame
Driver.SwitchTo().DefaultContent();
Driver.SwitchTo().Frame("border_bottom");
Driver.SwitchTo().Frame("xxx_info_new");
Basically, we can switch over the elements in frames using 3 ways.
By Index
By Name or Id
By Web Element locator
If XXXX_info_new is a child of any other frame then you need to switch to that and then current XXXX_info_new
So here you have name
Driver.SwitchTo().Frame("XXXX_info_new") // put the correct name as you have hide it in image
Source:
https://www.guru99.com/handling-iframes-selenium.html
This works
Driver.SwitchTo().Frame(border_bottom).SwitchTo().Frame(xxx_info_new);

Get element by using whats inside that element [duplicate]

This question already has answers here:
How do I find an element that contains specific text in Selenium WebDriver (Python)?
(12 answers)
Closed 3 years ago.
I want to access an element using selenium web driver using the inside text of a button element.
I want to catch this button element using "next" value.
<button>next</button>
How can I do this?
You can use following xpath options to get the element with text next
Option1:
//button[text()='next']
Option2:
//button[contains(.,'next')]
Option3:
//button[contains(text(),'next')]

How to switch from child iframe to main iframe and again from main iframe to normal frames?

I have a nested iframe. I am in child frame currently,Now i want to switch to main iframe and then from mainframe i want to switch to normal frames.How to do this?
Scenario : Nested iframe.
iframeMain
iframeParent
iframechild
Assume you are in ifrmaechild :
When you do driver.switchTo().parentFrame(); : you will go to iframeParent .
But when you do driver.switchTo().defaultContent(); : you will go to main HTML of page.
Note that in this case you will not go to iframeMain.
So, in your case you should do : driver.switchTo().parentFrame(); to go to iframe parent. and from here you should use : driver.switchTo().defaultContent(); to go to default DOM tree.
Hope this will help.
If you are in the child frame, switch to default content like this:
driver.switchTo().defaultContent();
Now you are on the main HTML and you can switch to every frame again. If you want to switch on nested frames, you have to do it step by step.
More information you can get in this tutorial.
As per your scenario, Currently you are in childFrame, Now you want to go back to mainFrame from childFrame like childFram > mainFrame > childFrame.
Basically, we can switch over the elements in frames using 3 ways
By Index
By Name or Id
By Web Element
You can switch frame by index or cssSelector. I have also having same problem like you. I implemented resolution for my problem like below.
//Below will gives you number of frames available in current page.
Int size = driver.findElements(By.tagName("iframe")).size();
//Switch to frame by it's index
driver.switchTo().frame(4);
//To write text in iframe using below code.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
body.sendKeys("Some text");
//below code will switch to default DOM
driver.switchTo().defaultContent();
Hope, This will work for you.

How to Detect Dynamic frame in selenium webdriver?

I am trying to detect frame with dynamic target in selenium webdriver.
In selenium IDE i am getting details of frame like command "selectframe", Target="CitrixMainFrameWI_hghjghjhj355", In the target dropdown no other value is present ,
I tried to detect this frame using command
driver.switchto().frame("CitrixMainFrameWI_hghjghjhj355"). But this target value is dynamically generated so i got error .
Can you please suggest me any solution for this
Try it with frame(index) or by naming the frames and only searching for CitrixMainFrame
(contains("CitrixMainFrame").
More Details in this link:
http://joychester.blogspot.com/2010/09/switch-frame-and-windows-sample-code.html
Use this :
// First find the frame.
WebElement element = driver.findElement(By.cssSelector("div[id^= 'CitrixMainFrameWI_']"));
// id^ means the id starts with the given value.
// You din't specify the tags, so i assumed as div tag. change it according to your code.
driver.switchto().frame(element);
String expectedFrameID="abc";
List<WebElement> lst=d.findElements(By.tagName("iframe"));
System.out.println(lst.size());
int flag=0;
for(int i=0;i<lst.size();i++){
String actualFrameID=lst.get(i).getAttribute("id");
System.out.println(lst.get(i).getAttribute("id"));
if(expectedFrameID.equals(actualFrameID)){
flag=1;
break;
}
}
if(flag==1){//perform operation on frame}