Selenium webElement.clear() doesn't work with phantomJS but yes with Firefox - selenium

I am trying to use PhantomJS as a browser to make a selenium test, but I have a problem with an input.
This input is a calendar (is not type date, is text but modified by JavaScript).
First of all, I have this using FirefoxDriver and works perfectly:
webElement.Clear();
webElement.SendKeys(date);
but, using PhantomJSDriver jumps an exception which says this:
{"errorMessage":"Element must be user-editable in order to clear it.","request":{"headers":{"Accept":"application/json, image/png","Connection":"Close","Content-Length":"0","Content-Type":"application/json;charset=utf-8","Host":"localhost:53291"},"httpVersion":"1.1","method":"POST","post":"","url":"/clear","urlParsed":{"anchor":"","query":"","file":"clear","directory":"/","path":"/clear","relative":"/clear","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/clear","queryKey":{},"chunks":["clear"]},"urlOriginal":"/session/a1e4cdb0-b901-11e4-befc-c3b28b46e8f3/element/%3Awdc%3A1424437741971/clear"}}
is very extraneous because in Firefox works perfectly,
what can I do to send the data?
Also I tried to sen this data without clear it, but the default value is 20/02/2015 and when i send something just add after the current text: 20/02/201530/03/2015 and doesn't work.
attribute .text doesn't has setter, the same for getAttribute().
thanks for all,
Ivan.
#edit1: html
<input type="text" aria-haspopup="true" title="" value="20/02/2015" name="id0" id="id0" class="bg datetime hasDatepicker">

Finally i found how to do this, probably is not the best way, but in my case works.
I send the data by JavaScript with javascriptExecutor
((IJavaScriptExecutor)_driver).ExecuteScript("document.getElementById('id0').value='" + date+ "'");

Related

How to send a date value to read-only input boxes using VBA Selenium webdriver

I ran into an issue with a read-only calendar input box. I need to send a date value to it through Selenium VBA. Here is the snippet of the HTML code I'm looking at:
<input class="dxeEditArea dxeEditAreaSys" id="pickupDate_I"
name="ctl00$ctl00$MainPane$Content$MainContent$OPUserControl$pickupDate"
onfocus="ASPx.EGotFocus('pickupDate')" onblur="ASPx.ELostFocus('pickupDate')"
onchange="ASPx.EValueChanged('pickupDate')" type="text" readonly="">
I put a solution I ended up using as a JS code below as a separate answer.
Here is the solution I ended up using: run a JavaScript code to replace the value in the field, rather than using SendKeys. This way we bypass readonly attribute that affects .SendKeys, but using .Value inside the script works:
driver.ExecuteScript ("document.getElementById('pickupDate_I').value = '01/01/2010'")
driver.findElement(By.xpath("//input[#id='pickupDate_I'])).sendKeys("your value");

how to get the readonly field value using selenium

I have read only field on Webpage as
<div id="dspIdDescriptionDet-inputEl" class="x-form-display-field" role="input" aria-invalid="false" data-errorqtip="" style="width: 100%;">2010-RR3 XIIIA9 20360726 FLT</div>
When I'm trying to get the display value ("2010-RR3 XIIIA9 20360726 FLT") using getText() or getAttribute("Value") using Webdriver, its fetching nothing.
error on eclipse:
expected:<2010-RR3 XIIIA9 20360726 FLT> but was:<null>
code:
driver.findElement(By.xpath(".//*[#id='dspIdDescriptionDet-i‌​nputEl']")).getAttri‌​bute("value"))
For getting the text of an element which has a unique id try using:
driver.findElement(By.id("dspIdDescriptionDet-inputEl")).getText()
Moving my comment to an answer since it turned out to be the issue...
I would try adding a wait. It may be that the element is present but it takes a bit for the data to populate into the field.

unable to find the element in my application using selenium webdriver

HTML code for element is:
<select id="pt1:reg2:1:soc1::content" class="x2h" title="" theme="dark" name="pt1:reg2:1:soc1">
Every time the xpath is getting changed but i am unable to find element.
WebElement w1 = driver.findElement(By.xpath(".//select[starts-with(#id, 'pt1') AND contains(#id, ':soc1::content')]"));
Select s = new Select(w1);
s.selectByVisibleText("Commercial");
I tested the xpath with a online tool like: http://www.xpathtester.com/xpath
This query worked just fine, i changed the and to lowercase.
.//select[starts-with(#id, 'pt1') and contains(#id, ':soc1::content')]
Write Xpath in Double quotes "xpath here" and
Don't take first full stop '.' from xpath,its of no use.
It start with //
Good luck
".//select[starts-with(#id, 'pt1') AND contains(#id, ':soc1::content')]"
Here full stop(.) before // need to be removed in Xpath.
Below one is without "AND" which is also working fine.
//select[starts-with(#id, 'pt1')][contains(#id,':soc1::content')]

Selenium preceding-sibling::text() not working

I am having issues with selenium using the next xpath "./preceding-sibling::text()"
I don't understand why, my first thought was that IE wasn't supporting that xpath statement but it didnt work on chrome neither.
What I am trying to do is to verify if a radio button have a certain text "near" it. For example if a radio button is like this
<div>
<label> Yes <input name='radioBtn'></input></label>
<label> No <input name='radioBtn'></input></label>
</div>
This is a simplified scenario where I need to check the "yes" radio button, so what I am doing is to search for the radiobutton and check if it preceding-sibling::text(), but selenium is cant find any element. I know the Xpath works because I test it on chrome developer tools and it returns the text "Yes".
I can't do something like 'input[1]' because I can't be sure that the first one will be always that have the Yes text.
Any idea why isn't this working on selenium? and if there is any work around?
I got to a work around but is kind of specific to the problem. But let's answer the questions 1 at the time.
Any idea why isn't this working on selenium?
It's not working because selenium don't support text elements, so even when selenium find the element it cant map it to a selenium object, i didn't see it because my code hided the selenium exception. The Exception throw is the next one:
An unhandled exception of type
'OpenQA.Selenium.InvalidSelectorException' occurred in WebDriver.dll
Additional information: invalid selector: The result of the xpath
expression "./preceding-sibling::text()" is: [object Text]. It should
be an element
Is there any work around?
Yes it is. What I did was to run a java script using the selenium IJavaScriptExecutor. With the script I revised the preceding sibling text and return it as a string so if the text was equal to the thing I was looking for (i.e Yes) trhat means that is the radio button I was looking for.
The code looks is similar to this (it can have some sintax errors since I didn't copied from the source):
string script = "function GetPrecedingText(node){"+
"var result = document.evaluate('./preceding-sibling::text()[1]', node, null, XPathResult.STRING_TYPE, null);"+
"return (result==null) ? '' : result.stringValue.trim();}"+
"return GetPrecedingText(arguments[0])";
string result = ((driver as IJavaScriptExecutor).ExecuteScript(script, SeleniumElement)).toString();
Hope someone can find this useful :) and thanks for all that tried to help me :)

How to handle the button click in Selenium Webdrive?

I am new to WebDriver,and currently trying to write the code to click the button. The locator is not available so I have used Xpath,but it is not working as it should be. Kindly help me on this.
Button tag:
<button onclick="myFunction()">Try it</button>
My web drive code:
drive_url.findElement(By.xpath("html/body/button")).click();
Did you check your xpath in browser console. You can check xpath by writing $x("<your xpath>") in console. Try using "//button" instead of what you're using now.
You should be little careful writng the selector as well. Try to avoid flaky selector and make is as unique as possible.
By xpath = By.xpath("//button[contains(text(),'Try it')]");
drive_url.findElement(xpath ).click();
The above selector finds the button tag explicitly using text based search.
Try this:
WebElement btn = driver.findElement(By.tagName("button"));
String btnText= driver.findElement(By.tagName("button")).getText();
if(btnText.equals("Try it")){
btn.click;
}