Facing issues in Identify button in selenium - selenium

I am working on 1 project, where i have to locate button, i have used xpath for same, but button id change on every refresh, so i am facing problem in it..
Below is screenshot for same - let me know you can i identify that button so that it will not cause error even if id chages
<button type="button" id="ext-gen11" class=" x-btn-text">Login</button>
Code HTML
<tr>
<td></td>
<td><div id="LoginButton" style="float: left;"><table style="width: auto;" id="ext-comp-1032" class="x-btn x-btn-noicon x-btn-over x-btn-focus" cellspacing="0"><tbody class="x-btn-small x-btn-icon-small-left"><tr><td class="x-btn-tl"><i> </i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i> </i></td></tr><tr><td class="x-btn-ml"><i> </i></td><td class="x-btn-mc"><em class="" unselectable="on"><button class=" x-btn-text" id="ext-gen12" type="button">Login</button></em></td><td class="x-btn-mr"><i> </i></td></tr><tr><td class="x-btn-bl"><i> </i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i> </i></td></tr></tbody></table></div></td>
</tr>

xPath:
//button[text()='Login']

XPath should do the trick:
Command: clickAndWait
Target: //input[#value='Login']
Edit
Assuming, that there is only one button with value "Login" on it

Related

Selenium IDE to create fb group redactor (or div text input field)

I have some HTML with a (facebook Description text for an event) where I need to enter text, however I cannot find where or how I can add the text, which is a div, span or other. I think this is a redactor (but I'm no expert!)
<th class="_3sts">
<!-- react-text: 77 -->Description
<!-- /react-text -->
</th>
<td class="_480u">
<div class="_mh-">
<div class="_56ji _5yk1">
<div tabindex="-2" class="_5yk2">
<div class="_5rp7">
<div class="_1p1t _1p1u">
<div class="_1p1v">Tell people more about the event</div>
</div>
<div class="_5rpb">
<div style="outline: medium none; white-space: pre-wrap; word-wrap: break-word; background-color: transparent;" title="Tell people more about the event" spellcheck="false" role="combobox" class="_5rpu" aria-owns="js_g" aria-haspopup="false"
aria-expanded="false" aria-autocomplete="list" contenteditable="true">
<div data-contents="true">
<div data-offset-key="9c3am-0-0" data-editor="dvke2" data-block="true" class="">
<div class="_1mf _1mj" data-offset-key="9c3am-0-0"><span data-offset-key="9c3am-0-0"><br data-text="true"></span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
I have tried these in Selenium IDE:
<tr>
<td>type</td>
<td>//div[#title='Tell people more about the event']</td>
<td>testing</td>
</tr>
and
<tr>
<td>type</td>
<td>//th[text()="Description"]/../td/div/div/div/div/div/div/div/div/div/span/</td>
<td>test</td>
</tr>
and (to apply a redactor solution)
<tr>
<td>runScript</td>
<td>$(._5rpu).html('text');</td>
<td></td>
</tr>
But while I can easily identify the area (one of the many divs) that seem to reflect the space where the text should go I can't manipulate it. Any help appreciated! ;-)
That field is not a trivial case.
First of all it's not an input. That's why "type" command will not work even if you will find element properly.
Secondly the div is in dynamic change when you are entering text that's why you cant use sendKeys for long text because this command is sending symbols one by one to the element that was located once. So after the first successful symbol sending, DOM of the element will be changed and it will become impossible to finish sending other keys.
But you can send one symbol using sendKeys like that:
sendKeys | //div[#class='_5rpu'] | Y
Finally because of unknown reason you can't send symbols with series of sendKeys commands like:
sendKeys | //div[#class='_5rpu'] | Y
sendKeys | //div[#class='_5rpu'] | O
That will throw an exception
[error] Unexpected Exception: Error: Cannot set the selection end.
The only way I've found is to use an ancient and deprecated typeKeys. After typeKeys command you also need a click a whole div which is containing field because orthodox typeKeys is working incorrect with that strange field too.
Also you need to be fully sure that all the animations was showed before you will start. That's why I recommend a small pause before you will try to type to the field.
So here is the final and working code:
pause | 2
typeKeys | //div[#class='_5rpu'] | myDescription
click | //div[#class='_56ji _5yk1']

Webdriver FindElement by CssSelector Click Not Working

I have a dropdown on a web page for selecting a country which has been rendered using the jQuery Chosen plugin. An extract of the html below,
<div>
<label for="phMainContent_EmployeeAdd1_ddlCountry" id="phMainContent_EmployeeAdd1_lblCountry" class="short required">Country*</label>:
<div id="phMainContent_EmployeeAdd1_ddlCountry_chzn" class="chzn-container undefined chzn-container-single" style="width: 199.44444px;">
<span>Please select ...</span><div><b></b></div>
<div class="chzn-drop" style="left: -9000px; width: 197.222px; top: 28px;">
<div class="chzn-search"><input type="text" style="width: 162px;"></div>
<ul class="chzn-results">
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_0" class="active-result result-selected">Please select ...</li>
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1" class="active-result">United Kingdom</li>
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_2" class="active-result">Afghanistan</li>
.......
If I use the Selenium IDE to record the actions to select the “United Kingdom” from the list the following script is recorded. Run snippet to see table with the commands in it.
<table border="1">
<tr>
<td>Command</td>
<td>Target</td>
</tr>
<tr>
<td>click</td>
<td>css=a.chzn-single > span</td>
</tr>
<tr>
<td>click</td>
<td>id=phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1</td>
</tr>
</table>
I can run this script repeatably in the IDE and the UK is selected from the dropdown each time. However, if I export the C#/Nunit/Webdriver code below
driver.FindElement(By.CssSelector("a.chzn-single > span")).Click();
driver.FindElement(By.Id("phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1")).Click();
and execute it, it fails on the 1st statement with the Selenium Element Not Visible exception.
Any advice on how to resolve this issue?
you can try xPath and select like //span[contains(.,'Please Select')]
Use explicit wait to make sure the dropdown is visible before the click
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement dropdown = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a.chzn-single > span")));
dropdown.Click();
driver.FindElement(By.Id("phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1")).Click();

Selenium, XPATH to search for an Element in a table and Click on Radio button

I need to select an Item from the list of Table entries and click on Radio button. Radio button is on the left.
In the HTML Code below
Only Unique ID to look for a selection is the "TestSucscription"
I need to grep for this Value in the Table(It can have multiple Rows and It can be in any position) and then click on the Radio button on to its left.
<tr>
<td>
<input class="" type="radio" value="2ba068ff-b797-444e-b56b-a0ae0ae06f06" name="subscription[id]">
<div class="hide"></div>
</td>
<td> TestSubscription</td>
<td></td>
<td>Testing</td>
</tr>
<tr>
<td>
<input class="" type="radio" value="36a87684-9a65-4455-8605-dc5b5368fc23" name="subscription[id]">
<div class="hide">
<span class="error error-msg">This cannot be left blank.</span>
</div>
</td>
<td>SecurityProtection</td>
<td></td>
<td>CPN ChildProtection CPN ChildProtection</td>
</tr>
Please help me with this. I have tried below options in Webdriver(Java)
driver.findElement(By.xpath("//td[contains(text(), 'TestSubscription')]]/td/input[#type='radio']")).click();
I think Im going down the tree and coming back to click on Radio button which is why Its failing.
You can find the radio button with the following xpath:
//td[contains(text(), 'TestSubscription')]/../td/input
This matches the td with the text, goes up to the parent, then traverses to the input control you want to click.

Unable to Click on Element

I am unable to click on the element. I am able to locate it with xpath, the object with statements
"element.getText()"
returns correct values
"element.isDisplayed()"
returns true
but when i say "element.click()" throws an exception
"Element is not currently visible and so may not be interacted with"
The above exception is thrown in selenium 2.34 and higher versions.
When I use older selenium version say "2.25" it doesn't throw an exception but the click has no effect.
I am using FF browser on Win7 machine.Below is the HTML
<div class="dojoxGrid-row dojoxGrid-row-over row-read" style="">
<table class="dojoxGrid-row-table" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="dojoxGrid-cell " style="width:36px;" idx="0" tabindex="-1">
<td class="dojoxGrid-cell gridColFrom " style="width:150px;" idx="1" tabindex="-1">
<td class="dojoxGrid-cell gridColType " style="width:16px;" idx="2" tabindex="-1"/>
<td class="dojoxGrid-cell gridColAttach " style="width:16px;" idx="3" tabindex="-1"/>
<td class="dojoxGrid-cell gridColSub dojoxGrid-cell-over" style="width:400px;" idx="4" tabindex="-1">
<span style="white-space: nowrap;" title="(No subject)">(No subject)</span>
</td>
<td class="dojoxGrid-cell " style="width:72px;" idx="5" tabindex="-1">Tue May 13</td>
<td class="dojoxGrid-cell gridColHov " style="width:16px;" idx="6" tabindex="-1">
<td class="dojoxGrid-cell gridColFlag " style="width:16px;" idx="7" tabindex="-1">
</tr>
</tbody>
</table>
</div>
Element is not currently visible and so may not be interacted with
What you're seeing is commonly caused by multiple elements on the screen matching the search criteria. WebElement.findElement returns the first match, which may not actually be visible. You can check by calling "isDisplayed" on it.
Try to locate that element using same xpath in firebug and check whether it is showing element which is visible on screen.
Try to click element using jsExecuter:
JavascriptExecutor myJSExecutor = (JavascriptExecutor)myDriver;
myJSExecutor.executeScript("arguments[0].click();", myElement);
//where myDriver and myElement are already defined WebDriver and WebElement
It seems your application uses Dojo for UI. I faced same problem while clicking on an element which has dojo property and I solved it using JavaScriptExecutor in similar way as you mentioned above. The other solution to this problem is use .sendKeys(Keys.Enter).

XPath statement not executed in IDE

i faced a problem while replaying a script created for yahoomail page.The XPath statement to enter value in the "To" text field , is not working.Following are the XPath statements i used.
At the first try i used
<tr>
<td>type</td>
<td>to</td>
<td>mgtest#ymail.com</td>
</tr>
Second try was this statement
<tr>
<td>type</td>
<td>//div[#id= 'toid']/textarea[#id= 'to'][#name= 'to']</td>
<td>mgtest#ymail.com</td>
</tr>
Third try was this
<tr>
<td>typeKeysAndWait</td>
<td>//div[# id= 'composebox']/div[#id= 'toid']/textarea[#id= 'to'][#name= 'to']</td>
<td>mgtest#ymail.com</td>
</tr>
The result was like
[error] Element //div[# id= 'composebox']/div[#id= 'toid']/textarea[#id= 'to'][#name= 'to'] not found
similar result was obtained in the previous attempts.
Later when i tried
<tr>
<td>type</td>
<td>css=textarea.txtfield</td>
<td>mgtest#ymail.com</td>
</tr>
mail id was entered into the text field ,and the script worked perfectly.what might be the reason.Any thoughts.?
I am adding the XPath statements
<div id="composepage">
<div id="composebox" class="roundcorner">
<div id="errorContainer"/>
<input type="hidden" name="defFromAddress" value="mgtest#ymail.com"/>
<div class="fields row">
</div>
<div id="toid" class="row">
<label id="compose_to" for="to">
</label>
<textarea id="to" class="txtfield" name="to" autocomplete="off" tabindex="1" style="overflow: hidden; height: 19px;"/>
</div>
You have written invalid xpath query.
It should be
//div[# id='composebox']/div[#id='toid']/textarea[#id='to' and #name='to']
The <textarea> has an id attribute, which should be unique, so your first locator of simply to should work. It's possible that the element is not present or visible when your selenium command executes. I would recommend the following:
waitForVisible | id=to | 60000
type | id=to | mgtest#ymail.com
If your elements have unique ids, and you need to use XPath, you only need to be relative to the closest element with an id attribute.