Selenium waitForPopup with dynamic windowId? - selenium

When using Selenium how can I wait for a popup window if its id is dynamically generated?
For example:
selenium.click("link=mylink");
selenium.waitForPopUp("popup072815372337691199");
Obviously I cannot hardcode the window id in my source code. Any hints?

It would obviously be best to have a consistent or fully predictable window name, however if this is not possible you could try using the getAllWindowNames command to wait until the number of windows increments. If the name of the window is somewhat predictable (like a consistent prefix) you could then find out the full name of the new window before using waitForPopup or selectWindow.

do it this way....
'String href = selenium.getAttribute("link=myLink#herf");
selenium.openWindow(href, "myWindow");
selenium.selectWindow("myWindow");
selenium.click(...);
// do whatever
selenium.selectWindow(null); // go back to the previous window'

Related

Not able to locate webelement

I am not able locate a webelement, this web application opens in Internet explorer only and I have used all the possible ways to click but no luck.
Xpath locators that I have tried :
"//form[#id='Form1']//a[contains(text(),'Age Range')]"
and
"//form[#id='Form1']//a[#id='rptTables1_ctl07_hlTablename1']"
also I have tried click on the element using action class and javascript as well.
Attached DOM in the URL, please have a look here
In the node a, id value is not static so you can't locate that element using the id value but you can use partial id value for example, looks like rptTables1_ is unique in id value and the remaining part is changing so applying contains() on this may works.
Try the below xpath if there is only match :
//a[contains(#id, 'rptTables1_')]
Try the below xpath by providing the matching index if there are multiple xpath matches :
(//a[contains(#id, 'rptTables1_')])[Matching index number]
for example if the matching index is 3 then you can write like this (//a[contains(#id, 'rptTables1_')])[3].
Or you can use the Advanced Performance Parameters Panel Topics text to identify that element.
//a[contains(text(), 'Advanced Performance Parameters Panel Topics')]
Again if there are multiple matches then try to use indexing method as mentioned above.
Or you can try the below modified your xpaths :
//form[#id='Form1']//a[contains(#id, 'rptTables1_')]
or
(//form[#id='Form1']//a[contains(#id, 'rptTables1_')])[Matching index number]
or
//form[#id='Form1']//a[contains(text(), 'Advanced Performance Parameters Panel Topics')]
I hope it helps...
I haven't tried any automation in IE at all but in firefox, sometimes I encounter those issues and my work around other than the .click() function is send_keys(Keys.RETURN). Also, i'm using time.sleep(x) before clicking or sending keys to make sure the element has been loaded.

Selenium is not identifying the 'iframe'

Below in my code
driver.switchTo().frame(driver.findElement(By.id("file")));
Above line is not running
There is various way to switch to frame. Please share the HTML code if you need a exact code to switch to your respective application. you can try with below method. Better use index if your frame do not any name etc
driver.switchTo().frame(index)
replace index with 0 first and if it not work then try with 1 and then 2 etc one by one
More details
driver.switchTo().frame() has multiple overloads.
driver.switchTo().frame(name or id)
Here your iframe doesn't have id or name, so not for you.
driver.switchTo().frame(index)
This is the last option to choose, because using index is not stable enough as you could imagine. If this is your only iframe in the page, try
driver.switchTo().frame(0)
driver.switchTo().frame(iframe_element)
The most common one. You locate your iframe like other elements, then pass it into the method.
Here locating it by title attributes seems to be the best.
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='Fill Quote']")));
// driver.switchTo().frame(driver.findElement(By.xpath(".//iframe[#title='Fill Quote']")));
most probably, your iframe is not visible or your window is not active.
in Python:
driver.switch_to_default_content()
wait.until(EC.frame_to_be_available_and_switch_to_it("yourFrame"))
in Java:
driver.switchTo().defaultContent();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.tagName("yourFrame")));

How to foucs a new window (third party without windowName) in selenium IDE?

I am submitting search criteria through form to third party URL. I tried to give target="someName". But when i am trying to focus on new window using "sameName" and validating some text there, selenium unable to find that locator or focus on that window. retuning false. please help on this. its urgent.
I also tried storeAllWindowNames or Ids selenium commands, also not working its returning main window ID instead of new open window.
the idea is in the following. as you try to validate some text in a new window, I do not see any necessity to get window name as you are able to get the xpath or cssselector of the text to be validated using firepath (firebug addon in ffox) and using command
String txt = driver.findElement(By.xpath(//...blablbalba)).getText().trim();
//or
String txt = driver.findElement(cssSelecot(html>..blalblabla...)).getText().trim();
//validation
Assert.assertTrue(txt.equals("blablabla"));
you can actually validate it.
or if you want to validate something and selenium is not able to cope with it then I recommend you to use js (getText using js):
String cssSelector=....
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\""+cssSelector+"\");");
stringBuilder.append("return x.text().toString();") ;
String res= (String) js.executeScript(stringBuilder.toString());
Assert.assertTrue(res.trim().contains("blablabla....") );
Hope this works for you.
This is the solution:
Well, if you have no choice but use it. Here is one solution:
if you have a target with your own name like target="popup", then you only need open the target window before click on the link. Example:
[b]store "javascript{selenium.browserbot.getCurrentWindow().open('', 'popup')}", "myWindow"[/b]
If you have a target="_blank", you need change the target before open the window.
store "javascript{this.page().findElement('link=My link who open the popup with target blank').target='popup'}", "myVar"
store "javascript{selenium.browserbot.getCurrentWindow().open('', 'popup')}", "myWindow"
Then, you click on the link and selectWindow("popup"). That is.
Another solution is rewrite the entire A tag and change the href/target by a javascript window.open but this is a litle more work to do.

How can I store a dynamic text value in selenium ide and use it later in different window

I am new in selenium and trying to learning. I am creating a script and I got stuck here, I want to store a dynamic value from text message on web page ex. " Event Name:Test" this Event Name is dynamic and I want to store this and want to get in other window. In 2nd window i want to use this value(Test) to verify in the page
I tried storeValue, StoreText and storeAttribute command and getting error message for xpath or "element not found".
Please help me and advice me , what should I do?
Can You please suggest me the Xpath for storing and retrieving the event name. Please help me...
Thanks in advance,
Niru
If you are using the same test-case to navigate from page 1 to page 2 you can use the storeText function in the IDE to store the value of your event name. And then you can use the same variable in the page 2 for verification.
For example, in page 1 to store the value of event you use:
storeText(locator1, temp)
And then on page 2 you use assert:
assertText(locator2, ${temp})

Simulate TAB keypress event in Selenium RC

I need to simulate a tab keypress in Selenium RC, using the Java API.
I do this after having entered some text using:
selenium.type(input, "mytext");
I've tried 3 alternatives to get the tab working:
selenium.keyPress(input, "\\9");
and:
selenium.focus(input);
selenium.keyPressNative("09");
and even:
selenium.getEval("var evt = window.document.createEvent('KeyboardEvent');evt.initKeyEvent ('keypress', true, true, window,0, 0, 0, 0,0, 9,0);window.document.getElementsByTagName('input')[2].dispatchEvent(evt);")
The best I can get is a "tab space" to be inserted after my text so I end up with this in the input field:
"mytext "
What I actually want is to tab to the next control. Any clues? Thanks!
(Note: I have to use tab and can not use focus or select to chose the element I want to go to, for various reasons, so no suggestions along these lines please!)
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB + "");
I don't use the Java API, but this post from google groups suggests it is your solution. I can't imagine that "9" is different from "09" in your question, but give it a try?
Try the official TAB char: \t or \u0009
Some functions may used Onblur. It will trigger the function when the field lose the key focus. here we can use fireEvent with "blur" or "focus" command as follows:
command: fireEvent
target: id=your_field_identification
value: blur
Reference: http://qaselenium.blogspot.com/2011/01/how-to-triger-tab-key-in-selenium.html
Improvising Ryley's answer, we can use
selenium.keyDownNative(java.awt.event.KeyEvent.VK_TAB + "");
selenium.keyUpNative(java.awt.event.KeyEvent.VK_TAB + "");
I tried this method for VK_CONTROL in IE and it worked good.
Use typeKeys():
Quoting the above link:
Unlike the simple "type" command, which forces the specified value into the page directly, this command may or may not have any visible effect, even in cases where typing keys would normally have a visible effect. For example, if you use "typeKeys" on a form element, you may or may not see the results of what you typed in the field.
In some cases, you may need to use the simple "type" command to set the value of the field and then the "typeKeys" command to send the keystroke events corresponding to what you just typed.