I'm unable to perform actions on iframe window - java

Below is the site,
http://www.mortgagecalculator.org/
click on Get Today's Best Mortgage Rates & you will get one popup window,which
is there in frame. select purchase radio button and click on search button.
Here is my code. I am unable to select the radio button with my code. need suggestion. Thanks
driver.get("http://www.mortgagecalculator.org/");
driver.findElement(By.xpath(".//*[#id='calc']/form/section/section[2]/div/div/div[1]/div/div/div[3]/div[1]/div[1]/div[4]/a/strong/font")).click();
Thread.sleep(3000);
driver.switchTo().frame("brbodxlxcs");
Thread.sleep(4000);
System.out.println("***");
driver.findElement(By.xpath(".//*[#id='brTabbedRateTable']/div[1]/form[2]/div/div[3]/div[1]/ul/li[1]/input")).click();
driver.findElement(By.xpath(".//*[#id='brTabbedRateTable']/div[1]/form[2]/div/div[3]/div[2]/ul/li[8]/a")).click();
Thread.sleep(2000);

There is browser security related to iframes.
You cannot get access to the elements in an iframe like you can other parts of your page.
Others have struggled with this also:
Selecting an element in iFrame jQuery

Change driver.switchTo().frame("brbodxlxcs"); to match something like driver.switchTo().frame(driver.findElement(By.tagName("iframe[title='Fill Quote']")));

The frame that you are referring to brbodxlxcs , seems like id of this frame is dynamically changing.
Try this, its working for me
driver.get("http://www.mortgagecalculator.org/");
driver.findElement(By.xpath(".//*[#id='calc']/form/section/section[2]/div/div/div[1]/div/div/div[3]/div[1]/div[1]/div[4]/a/strong/font")).click();
Thread.sleep(8000);
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
System.out.println(frames.size());
Iterator<WebElement> it = frames.iterator();
while(it.hasNext())
{
WebElement temp = it.next();
System.out.println(temp.getAttribute("id"));
}
This gives me all the available frames, in this case its 5.
Then i tried for different frames available one by one & finally got this to work by
driver.switchTo().frame(2);
also i modified xpath for radio button like
.//*[#id='brTabbedRateTable']//form[#name='mtgSearchForm']//input[#name='loantype' and #value='purchase']
& Search button like this,
.//*[#id='brTabbedRateTable']//form[#name='mtgSearchForm']//a[text()='Search' and #class='br-submit']
I hope it helps !!

It looks the problem you are having is that the iFrame id is dynamic and is different every time the page is rendered. There are two iFrames on the page so use an xpath like to one below to find a specific instance:
(//iframe)[2]

Related

I want to check whether frame exists or not on web page by using Selenium, how can do it

After login to my application, sometimes a frame opens, which requires a click on the 'OK' button.
So, I have written the code below which switches to the frame, clicks on the OK button and again switches to default.
driver.switchTo().frame(driver.findElement(By.id("InlineDialog_Iframe")));
driver.findElement(By.id(prop.getProperty("pending_email_close_btn_id"))).click();
driver.switchTo().defaultContent();
But, if frame doesn't appear then code gives error saying that the frame doesn't exist.
Please let me know how can I check whether the frame exists or not by using 'if' loop or by any other method?
Thanks.
hi there i had to do some stuff in the past regards to iframes as well and it was very confusing for me as well,
so the very first within you need to understand is that the iframe is actually a web page "inside other web-page" due so you will need to switchTo().frame(...stuff..) to be able to do something
than when you finish you need to get the initial frame driver.switchTo().defaultContent(); so you king of move from one page to the other
regards to find the element which you are after in the iframe i suggest you find any element which will always be present and wrap it with a try
i also suggest you to create a class which will hold your code and wait for JS, Angular, Jquery ...
example from my code:
try {
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id^='xdm_default']")));//change focus to iframe
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.tagName("html"))));//wait for html in the iframe
WebElement divCardsGrid = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("div[class^='CardsGrid']"))));
if (!divCardsGrid.findElements(By.tagName("a")).isEmpty()) {//check for external links
} catch (WebDriverException e) {
log.error(e.getMessage());
}
//change the focus to the initial frame
driver.switchTo().defaultContent();
I hope that helps
List<WebElement> iframes = driver.findElements(By.tagName("iframe"));<br/>
if(iframes.size() > 0)<br/>
{<br/>
// Frames is Present <br/>
driver.switchTo().frame("a077aa5e"); //switching the frame by ID<br/>
driver.findElement(By.id(prop.getProperty("pending_email_close_btn_id"))).click();<br/>
driver.switchTo().defaultContent();<br/>
}else{<br/>
// No Frames <br/>
driver.findElement(By.xpath("html/body/a/img")).sendKeys(“----------”);<br/>
}<br/>

How to click button using selenium webdriver java, whose xpath is not constant

I am new to selenium webdriver in Java. I am using webdriver with Firefox Quantum (59.0, x64). The problem I am facing is that the website i am writing code for testing dose not have constant xpath. On every visit the button id changes. Even the classname(launchbutton) is same for all 4 Launch Buttons. So while writing code it initially worked & opened the first link, but for second link it again opened the first link as the classname is same for all 4 buttons. Please help me out to code to open other links too by pressing other launch button.
i used this(below) code but it worked partially. Please help me out as i want to launch using button of other subjects too. (Please refer screenshot)checkout screenshot1 here
checkout screenshot2 here
Also i am not able to switch between tabs. pls help.
thankyou
driver.findElement(By.className("launchbutton")).click();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
driver.get("http://google.com");
//Grab all elements which have the className "launchbutton"
List<WebElement> elements = driver.findElements(By.className("launchbutton"));
//Iterate your list of matching elements and look at the innerText for the button you want
//When found, click it and break the for loop
foreach(WebElement ele in elements)
{
if(ele.getAttribute("innerText") == "The button text you want")
{
ele.Click();
break;
}
}
You can click the Launch button as below and please change the code based on the mentioned comments
//Specify the Expected Name in the below String
String subjectName="";
//Find the Table Element using any one of the Unique Locator
WebElement table=driver.findElement(By.id(""));
List<WebElement> rowElementList=table.findElements(By.xpath(".//tr"));
//I have assumed Launch button column in 5th.So, Specified as td[4].
//Please cross check the Launch button column and change the index accordingly
for(WebElement element:rowElementList){
if(element.findElement(By.xpath("./td")).getText().equalsIgnoreCase(subjectName)){
element.findElement(By.xpath("./td[4]/a")).click();
}
}

Selenium: select an item in combobox

I have been trying to perform a selenium task on it:
In this page, there is a button which i have to click on it and then wait for 10 seconds. I did it like this:
Naviagation to page:
base.driver.navigate().to("http://suvian.in/selenium/1.7button.html");
Click on button:
//base.driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div/h3[2]/a"));
base.driver.findElement(By.linkText("Click Me"));
This step fails
Wait for 10 seconds:
TimeUnit.SECONDS.sleep(waitTime);
Questions:
1-it fails on clicking on the button. Although, i asked to find the link both with xpath, and text it cannot find it?
2-Is my solution correct for make a delay on webdriver's activity?
Try this code using xpath locator.
Note: Instead of using absolute xpath, use relative xpath.
new Select(driver.findElement(By.xpath("//select[#name='gender']"))).selectByVisibleText("Male");
OR
new Select(driver.findElement(By.xpath("//select[#name='gender']"))).selectByValue("1");
In the dropdown there are 3 options. The first one is Select, Male is the second. Try
select.selectByIndex(1);

WebDriver can't get dropdown menu element (Java)

I write a script on Java for Selenium WebDriver, and I have a problem with selected from dropdown menu.
Here's my locator:
new Select(driver.findElement(By.id("FormElement_select_68_input_input"))).selectByVisibleText("Image");
Here's an error: http://prntscr.com/7jul03
Here's HTML code: http://prntscr.com/7jvou6
Need to select "Image" from this menu, but have an error.
Before I had the error like this, I can't upload file, it was because I need to switch to frame(0).
But here I don't know why I can't select menu "Image" from DropBox.
Your ID is dynamic, so you can't use it. Select will not work in your case, you just need to use two clicks
WebElement dropdown = driver.findElement(By.xpath("//div[#class='select-pad-wrapper AttributePlugin']/input"));
dropdown.click();
WebElement element = driver.findElement(By.xpath("//div[#class='select-pad-wrapper AttributePlugin']/div/ul/li[text()='Image']"));
element.click();
It looks like the element id you're looking for"FormElement_select_68_input_input" doesn't exist in your html, your code sample shows "FormElement_select_283_input_container" as the select box element. Try this:
Select droplist = new Select(driver.findElement(By.Id("FormElement_select_283_input_container")));
droplist.selectByVisibleText("image");
Because it is not Select tag.
Try with below logic
WebElement div = driver.findElement(By.cssSelector("div[id*='FormElement_'] > div > div"));
div.click();
WebElement li = div.findElement(By.xpath(".//ul/li[text()='Image']"));
li.click();
As per HTML code screen, i am expecting Select class (selectByVisibleText etc) does not work. can you do one thing, try to click on required option directly. (may be click on "//div[#class='selectbox-wrapper']/ul/li[#class='selectbox_li'][contains(text(),'Image')]" , check one is it correct or not in firepath)
Let me know the result.. if it does not work, as said above you need to click on that input dropdown box and need to click on that Image.
Thank You,
Murali

Finding and switching to the right frame with Selenium Webdriver

I am trying to write a WebDriver test for the following site for learning purposes: http://www.mate1.com
On clicking the login link, I get a form to fill in my email id and password. As far as I understand, the form is shown in an iframe.
To enter the credentials, I tried to identify the number of iframes for that particular page (and found it as 7) and tried switching in to each iframe and search for the email field by XPath and the ID. However, I wasn't successful to find it. So how can this be done?
This is my code:
driver.get("http:www.mate1.com");
driver.findElement(By.xpath("//*[#id='header-container']/div/a")).click();
List <WebElement> frames = driver.findElements(By.tagName("iframe"));
System.out.println("Totalframes -> "+ frames.size());
driver.switchTo().frame(0);
driver.findElement(By.xpath("//[#id='email']")).sendKeys("xxxxxxxxx#rocketmail.com");
This is likely a good situation to use switchTo().frame(WebElement):
driver.switchTo().frame(findElement(By.cssSelector(".iframe-container>iframe")));
The login and password fields are not in an iframe, you can directly use the following code -
driver.findelement(By.id("email").sendKeys("xxxxxxxxx#rocketmail.com");
Try not to switch to any iframe and execute the above line, this will work.

Categories