Finding and switching to the right frame with Selenium Webdriver - java

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.

Related

I'm unable to perform actions on iframe window

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]

Selenium Webdriver - Unable to find element after page refresh/redirect

I am trying to write some UI test cases for an SAP-webUI (web based) application. After login, it shows the dashboard ( Workcenter's ) screen.
Now the problem is, I am able to open the page, enter U/N, Pwd and login through Selenium. After i press the "Login" button the URL changes and the page got redirected/refreshed.
E.g. URL before login : https://a/b/c/d/e/f/g.htm?sap-client=001&sap-sessioncmd=open
E.g. URL after successful Login : https://a/b(bDsdfsdsf1lg==)/c/d/e/f/g.htm
After this im unable to perform any action or press any link in any part of the page. I tried with all the possible attributes ( css, xpath, id ). Webdriver was unable to find any element on the page. It shows the error "No element found" alone.
I am using java with Selenium Web Driver.
Please find the html structure of the webpage below
<html><body><div><div><iframe>#document<html><head></head><frameset><frameset><frame>#document<html><head></head><body><form><div><div><table><tbody><tr><td><div><ul><li><a id=abcdef></a></li></ul></div></td></tr></tbody></table></div></div></form></body></html></frame></frameset></frameset></html></iframe></div></div></body></html>
Actually i want to click a linkmenu "abcd", which is inside iframe and frame as shown in the below HTML code
<html><head></head><body><iframe name=a1><html><head></head><frameset><frameset name=fs1><frame name=f1><html><head></head><body><table><tbody><tr><td><ul><li><a id=abcdef>
I tried the below code as well.
driver.switchTo().frame("a1");
driver.findElement(By.id("abcd")).click();
OR
driver.findElement(By.xpath("//*[#id='abcd']")).click();
After using the above code, still im getting the error "No such element"
Kindly suggest
Regards,
Siva
Do it this way...
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#name='a1']"))); // switching to iframe
followed by
driver.switchTo().frame("f1"); // switch to frame
and then your desired action...
driver.findElement(By.id("abcd")).click();
This is because of the iframe. You need to switch to it first:
driver.switchTo().frame(0);
driver.findElement(By.id("abcdef")).click();
where 0 is a frame index.
See doc on implicit wait here
I guess you should do a implicit wait until your chosen element is available
modify these code to suit your chosen element:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

getByXpath() not working inside frame

i am new to Htmlunit and trying to extract data from a website http://capitaline.com/new/index.asp. I have logged into the website successfully. When we log into website there are three frames.
One on the top to search for the company(like ACC ltd.) for which we are extracting data.
2nd frame has a tree which provide links to various data we want to look at.
3rd frame has the resulted data outcome on the basis of link you clicked in frame.
I managed to get the frame i need below:
HtmlPage companyAtGlanceTopWindow =(HtmlPage)companyAtGlanceLink.click().getEnclosingWindow().getTopWindow().getEnclosedPage();
HtmlPage companyAtGlanceFrame = (HtmlPage)companyAtGlanceTopWindow.getFrameByName("mid2").getEnclosedPage();
System.out.println(companyAtGlanceFrame.toString()); // This line returns the frame URL as i can see in my browser.
Output of print statement is
HtmlPage(http://capitaline.com/user/companyatglance.asp?id=CGO&cocode=6)#1194282974
Now i want my code to navigate down to the table inside this frame and for that i am using getByXPath() but it gives me nullPointerException. Here is the code for that.
HtmlTable companyGlanceTable1 = companyAtGlanceFrame.getFirstByXPath("/html/body/table[4]/tbody/tr/td/table/tbody/tr/td[1]/table");
My XPath for the current webpage(after i clicked the link)from which i am trying to extract table is seems correct, as it is copied from chrome element inspect. Please suggest some way to extract the table. I have done this type of extraction before but there i had id of table so, i used it.
Here is the HTML code for the table in the webpage.
<table width="100%" class = "tablelines" border = "0" >
I want to know that can you see the inner contents of each iframes in console (print asXml()), are they nested iframes?
well try this
List<WebWindow> windows = webClient.getWebWindows();
for(WebWindow w : windows){
HtmlPage hpage = (HtmlPage) w.getEnclosedPage();
System.out.println(hpage.asXml());
}
once you can see the contents,
HtmlPage hpage = (HtmlPage)webClient.getWebWindowByName(some_name).getEnclosedPage();
then using xpath grab your table contents(make sure your xpath is correct). It will work.(worked for me)
Thank you RDD for your feedback.
I solved the problem. Actually issue was not with the frame but with the XPath provided by chrome.
XPath Provided by chrome is:
/html/body/**table[4]**/tbody/tr/td/table/tbody/tr/td[1]/table
But the XPath worked for me is:
/html/body/**table[3]**/tbody/tr/td/table/tbody/tr/td[1]/table
It seems as, XPath provided by chrome has some glitch when there is a table within the path(Or may be some bug in htmlunit itself). I did many experiments and found that chrome always gives ../../table[row+1]/.. as XPath, while working XPath for htmlunit is ../../table[row]/..
SO, this code is working fine for me
HtmlTable companyGlanceTable1 = companyAtGlanceFrame.getFirstByXPath("/html/body/table[3]/tbody/tr/td/table/tbody/tr/td[1]/table");

Selenium Webdriver iframe interactions are inconsistant

When I enter an iframe (to provide Credit Card Details) using
driver.switchTo().frame(iframe);
It does its job correctly and filling in the form in the iframe and I switch out of the iframe using
WebElement ccInfo = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(".//*[#id='paymentech_form']/div/button"))));
ccInfo.click();
driver.switchTo().defaultContent();
However, the behavior of the function is inconsistent. The problem is when I am running the test in the background (minimized or hidden window) it always fails to click on the button mentioned above while the rest of the form is filled in. This problem does not prevail when I have the window open and selected.
Is there a possible work around for this?

Cannot find element in the popup window with Selenium Webdriver

I'm using InternetExplorerDriver with Selenium Webdriver in Java.
I load a URL which simultaneously opens another window which has the login box.
Using the method mentioned here
I could select the required window and also send keys into the focussed box.
popup.getKeyboard().sendKeys("yeuiryuiryweuiryeuiyterui");
However, the following:
popup.findElement(By.id("userName")).sendKeys("user")
is unable to find the element and throws an Element-not-found exception (something similar to this).
Where am I getting it wrong?
I will suggest you to try with xpath
String locator = "//*[#class='Class name of user name text box'and text()='if any']"
you can find the user name through firebug
List<WebElement> element = Driver.driver.findElements(By.xpath(locator));
thn use click functions on web element , do the same on password and then click on login directly using Driver.selenium.click(xpath); and provide the xpath of login button in case id's are not present.

Categories