How to switch between multiple frames using Selenium and Java - java

I want to send keys to Card Number, Expiration Date and CVV text fields which are in iframe.
Now what I observed is, when in the test case, whichever frame I write first to switch is located and the keys are sent and other two are ignored.
In below code, I mentioned expiration date frame first which is located but the card frame i.e. cddnumber id frame is not found.
cpp.fillintextfields.get(4).sendKeys("test#test.com");
WebElement es = driver1.findElement(By.id("CollectJSInlineccexp"));
driver1.switchTo().frame(es);
cpp.expdate.sendKeys("01/21");
driver1.switchTo().frame("CollectJSInlineccnumber");
Thread.sleep(2000);
cpp.cdnumber.sendKeys("4111111111111111");
Thread.sleep(5000);
Now when I mention cddnumber i.e. card number frame first as in code below and expiration date frame after that, card number frame is located and expiration date ones is not located.
cpp.fillintextfields.get(4).sendKeys("test#test.com");
driver1.switchTo().frame("CollectJSInlineccnumber");
Thread.sleep(2000);
cpp.cdnumber.sendKeys("4111111111111111");
Thread.sleep(5000);
WebElement es = driver1.findElement(By.id("CollectJSInlineccexp"));
driver1.switchTo().frame(es);
cpp.expdate.sendKeys("01/21");
Following are the TestNG traces of error given in short when I mention expiry date frame before card number frame:
org.openqa.selenium.NoSuchFrameException: No frame element found by name or id CollectJSInlineccnumber
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'KE5', ip: '10.6.6.105', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '10.0.2'
Driver info: driver.version: unknown
at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.frame(RemoteWebDriver.java:885)
Please help me and suggest me any solution to tackle this issue so that all the frames can be located even though I write them one after the other.

When you need to switch between two child frames of the same parent frame (e.g. Top Level Frame) you need to switch to the defaultContent which is either the first frame on the page, or the main document when a page contains iframes and then switch to the second child frame as follows:
First code block:
cpp.fillintextfields.get(4).sendKeys("test#test.com");
WebElement es = driver1.findElement(By.id("CollectJSInlineccexp"));
driver1.switchTo().frame(es);
cpp.expdate.sendKeys("01/21");
driver1.switchTo().defaultContent();
Thread.sleep(2000);
driver1.switchTo().frame("CollectJSInlineccnumber");
Thread.sleep(2000);
cpp.cdnumber.sendKeys("4111111111111111");
Second code block:
cpp.fillintextfields.get(4).sendKeys("test#test.com");
driver1.switchTo().frame("CollectJSInlineccnumber");
Thread.sleep(2000);
cpp.cdnumber.sendKeys("4111111111111111");
Thread.sleep(5000);
driver1.switchTo().defaultContent();
WebElement es = driver1.findElement(By.id("CollectJSInlineccexp"));
driver1.switchTo().frame(es);
cpp.expdate.sendKeys("01/21");
References
You can find a couple of relevant discussions in:
Multiple iframe tags Selenium webdriver

Related

Selenium/Java - Cannot locate option with index

I am practicing selenium with practiceautomation.com site. I have problem with registration - my selector cannot locate option with index.
code:
Select yearSelector = new Select(driver.findElement(By.id("years")));
yearSelector.selectByIndex(2000);
And I got something like:
org.openqa.selenium.NoSuchElementException: Cannot locate option with index: 2000
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'DESKTOP-NN5LV43', ip: '192.168.0.2', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10.0.1'
Driver info: driver.version: unknown
It is strange, because I use also
Select dateSelector = new Select(driver.findElement(By.id("days")));
dateSelector.selectByIndex(15);
And everything works fine and the day on the list is normally selected
Picture:
Year
As you can see, years are visible.
You try to select by index 2000 and errors says no option with index 2000. Maybe you want to select by value?
Select yearSelector = new Select(driver.findElement(By.id("years")));
yearSelector.selectByValue("2000");
public void selectByIndex(int index)
The above method select the option at the given index. This is done by examining the "index" attribute of an element, and not merely by counting.If no matching option elements are found then NoSuchElementException is thrown
Check for index attribute in html with value as 2000 it should not be there so try with
selector.selectByIndex(1); // see first visible year is selected or not

Scroll and click - Not working for both Android and iOS on Appium tool with selenium java

I am trying to scroll and its not working for both Android and iOS, Can you please help me on this.
Look forward to hear back from you.
Did try with ScrollTO and ScrollToExact as both of them are deprecated now, so did try with this:
String str = "CADILLAC";
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+str+"\").instance(0))").click();
Still does not work.
Did go through many blogs , videos , course content and material no one has explained or there a specific solution on how to handle this
Did try with below,still gets the error
org.openqa.selenium.WebDriverException:
driver.findElementByAndroidUIAutomator("UiSelector().resourceId(\"current_value_tv\").text(\"All Makes\")").click();
driver.findElementByAndroidUIAutomator("UiSelector().className(\"android.widget.TextView\").text(\"AUSTIN HEALEY\")").click();
org.openqa.selenium.WebDriverException: An unknown server-side error
occurred while processing the command. (WARNING: The server did not
provide any stacktrace information)
Command duration or timeout: 45 milliseconds
Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
System info: host: 'NCA1026471', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version:
'1.8.0_91'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=C:\Appium\workspace\Lokesh\app\app-release.apk,
appPackage=au.com.carsguide, rotatable=true,
networkConnectionEnabled=true, noReset=true, warnings={},
handlesAlerts=true, deviceName=Carsguide Product, version=0.17.0,
platform=ANDROID, appActivity=au.com.carsguide.activity.HomeActivity,
desired= {app=C:\Appium\workspace\Lokesh\app\app-release.apk,
appPackage=au.com.carsguide,
appActivity=au.com.carsguide.activity.HomeActivity, noReset=true,
platformVersion=5.0, browserName=, platformName=Android,
deviceName=Carsguide Product, device=Android}, acceptSslCerts=true,
platformVersion=21, automationName=selendroid, browserName=selendroid,
takesScreenshot=true, javascriptEnabled=true, platformName=android,
device=Android}]
Session ID: e90cac4d-38aa-99fd-2dd2-70cc09a0e717
*** Element info: {Using=-android uiautomator, value=UiSelector().resourceId("current_value_tv").text("All Makes")}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
you can try this below code, i was trying this code on settings page..
AppiumDriver driver = new AndroidDriver(new URL(""), cap1);
driver.scrollTo("About phone");
Pass the String which is present in the bottom of your app page.
driver.scrollTo("Enter your value");
Use appropriate wait Statements.
use driver.swipe instead of driver.scroll
http://automationbyharsh.blogspot.in/
visit this blog to get full documentation of swipe method for Android and IOS.
scrollTO method is not consistent in appium for android apps. so try the following.
go till model listing page
try the below code snippet , This was for flipkart app
it may help you with your app
driver.findElementById("com.flipkart.android:id/search_autoCompleteTextView").sendKeys("Reebok Shoes" +"\n");
Thread.sleep(8000);
Dimension size = driver.manage().window().getSize();
System.out.println(size);
int Starty=(int)(size.height*0.90);
int Endy=(int)(size.height*0.10);
int Startx=(int)(size.width*0.50);
WebElement ele_item = driver.findElementByClassName("android.widget.TextView");
Thread.sleep(3000);
do
{
List<WebElement> ele_item2 = driver.findElementsByXPath("//*[#index='2'][#text='Reebok SPEED XT Running Shoes'][#class='android.widget.TextView']");
int size2 = ele_item2.size();
System.out.println(size2);
if(size2>0)
{
driver.findElementByXPath("//*[#index='2'][#text='Reebok SPEED XT Running Shoes'][#class='android.widget.TextView']").click();
break;
}
driver.swipe(Startx, Starty, Startx, Endy, 1000);
Thread.sleep(2000);
} while(ele_item.isDisplayed()==true);

Select a specific iFrame that has multiple instances of the same name

My problem is I have 3 defined iFrames on the page I am trying to work on. They are all named the following:
<iframe class="col-51" height="560" src="https://attendee.gototraining.com/embed/886b2/catalog/40xxxx bgColor=ffffff" frameborder="0"></iframe>
<iframe class="col-51" height="560" src="https://attendee.gototraining.com/embed/886b2/catalog/37xxxx bgColor=ffffff" frameborder="0"></iframe>
<iframe class="col-51" height="560" src="https://attendee.gototraining.com/embed/886b2/catalog/46xxxx bgColor=ffffff" frameborder="0"></iframe>
I have tried to driver.switchTo.frame command but it only sees index 0 which I assume is the first frame listed here. If I select this first frame I don't get an error at runtime, however I still cannot see any elements inside the frame. I get "cannot find element" issue. I cant even select the 2nd or 3rd frame with switchTo using either a located I make or index 1 or 2.
I have also tried making a List involving all tagnames of iframe. I get back only 1 result. Again, I see 3 here. And again, even when I choose the first one I still cannot see any elements inside the frame using conventional means.
What am I missing here?
Also please note I have put x's in for the pathway because of privacy concerns.
Here is a capture of what happens if I choose another index other than 0:
org.openqa.selenium.NoSuchFrameException: Unable to locate frame: 1
Command duration or timeout: 62 milliseconds
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'DESKTOP-1PV0EPA', ip: '192.168.0.11', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
You can use xpath locator and identify a frame by partial src link:
void switchToIframe(int catalogId)
{
driver.switchTo().defaultContent(); // don't get trapped in iframes
driver.switchTo().frame(
driver.findElement(
By.xpath("//iframe[contains(#src, 'catalog/" + catalogId + "')]"));
}
use it like:
switchToIframe(40); // frame 1
switchToIframe(36); // frame 2
switchToIframe(46); // frame 3
you may also need to wait for frames to load before switching to them
I would suggest trying to resolve the iframes as WebElements by their XPaths. Code snippet below stubs out the test case, you'll need to determine the xpaths to each of your iframes and fill the strings in, but I think this should be close.
private static final By FRAME_PATH_1 = By.xpath("");
private static final By FRAME_PATH_2 = By.xpath("");
private static final By FRAME_PATH_3 = By.xpath("");
private void traverseFrames(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement frame1 = wait.until(ExpectedConditions.presenceOfElementLocated(FRAME_PATH_1));
WebElement frame2 = wait.until(ExpectedConditions.presenceOfElementLocated(FRAME_PATH_2));
WebElement frame3 = wait.until(ExpectedConditions.presenceOfElementLocated(FRAME_PATH_3));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame1));
System.out.println("in Frame 1");
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame2));
System.out.println("in Frame 2");
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame3));
System.out.println("in Frame 3");
}
XPaths are not great for maintainability if you expect the page contents to change, but it may get you closer to what you're looking for. I use the Firepath extension for Firebug to help resolve xpaths.
Best of Luck

org.openqa.selenium.NoSuchElementException: Unable to locate element:

Code:
public void Test2() throws Exception{
Thread.sleep(5000);
driver.findElement(By.id("cboMenu")).click();
driver.findElement(By.xpath(".//*[#id='cboMenu/option[3]")).click();
}
Error:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"cboMenu"}
Command duration or timeout: 31 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:03'
System info: host: 'venu-PC', ip: '192.168.1.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Session ID: 0f859bed-35df-4eba-a472-3bc2efec4814
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Please use explicit wait instead of the Thread.sleep(5000), like in the next example.
It will provide you much clearer error regarding what you experience.
public void Test2() throws Exception{
new WebDriverWait(driver, 3).until(ExpectedConditions.visibilityOfElementLocated(By.id("cboMenu")))
driver.findElement(By.id("cboMenu")).click();
driver.findElement(By.xpath(".//*[#id='cboMenu/option[3]")).click();
}
Next, verify your button doesn't appear in different iFrame. If you do, change the iFrame to the one you element inside:
driver.switchTo().frame("IFRAME_ID");
The IFRAME_ID is taken from the DOM:
<iframe id="IFRAME_ID">
You can next change visibilityOfElementLocated to presenceOfElementLocated, that will verify that an element is present on the DOM but does not necessarily mean that the element is visible. It can be good clue to know if your webDriver is in the correct scope with the button you try to click.
Additional tip - scroll the button you want to click on into view. Thats could be also the reason to failure.
//element is WebElement
(JavascriptExecutor)driver.executeScript("arguments[0].scrollIntoView(true);", element);
this is solved my issue :)
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.context("WEBVIEW_com.openstream.cueme.services.workbench");
Thread.sleep(10000);
driver.findElementById("userId").sendKeys("sysadmin");
driver.findElementById("CuemePassword").sendKeys("MMNext13#");
Try below code
public void Test2() throws Exception{
Thread.sleep(5000);
driver.findElement(By.id("cboMenu")).click();
driver.findElement(By.xpath(".//*[#id='cboMenu']/option[3]")).click();

Search using Selenium WebDriver works on google but Fails on yahoo

All:
I am running a simple Java application with Selenium WebDriver.
I was able to successfully run a search
on http://www.google.com using org.openqa.selenium.htmlunit.HtmlUnitDriver
I tried to run the same search term on http://www.yahoo.com as shown in the following code excerpt:
CharSequence[] searchTerm = { "bbc", "news" };
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.yahoo.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
//searchTerm = "bbc news";
// Enter something to search for
element.sendKeys(searchTerm);
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
Howefver, it gives me the following error:
Oct 17, 2014 3:18:44 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Cookie rejected [DNR="deleted", version:0, domain:.www.yahoo.com, path:/, expiry:Thu Oct 17 15:18:45 IST 2013] Illegal domain attribute "www.yahoo.com". Domain of origin: "in.yahoo.com"
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:58'
System info: host: , ip: , os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_05'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1001)
Why does it work fine for http://www.google.com but fails for http://www.yahoo.com ?
Why does it throw the "Exception in thread main org.openqa.selenium.NoSuchElementException Unable to locate element with name q" error?
Update with Answer
Thanks to #Sriram and #ivan_ochc , I am able to run the following code that searches http://www.yahoo.com properly
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.yahoo.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("p"));
http://www.yahoo.com doesn't have element with name="q", it has element with name="p"

Categories

Resources