I am using WebDriver and java to get page source. Using FirefoxDriver I am trying to verify some text on the page source but when I use driver.getPageSource, its converting some of the signs such as < to $lt; and > to > because of which its hard for me to verify content.
Can someone please guide me how to avoid this?
<noscript>
<div id="noScriptContainer">
<p>JavaScript is not enabled! Either you have disabled it or your browser does not support it. Because of this, you will not be able to view our pages or use our site features. Please turn on JavaScript in your browser settings or upgrade your browser version to use our site. </p>
</div>
</noscript>
Converted to =
<noscript>
<div id="noScriptContainer">
<p>JavaScript is not enabled! Either you have disabled it or your browser does not support it. Because of this, you will not be able to view our pages or use our site features. Please turn on JavaScript in your browser settings or upgrade your browser version to use our site. </p>
</div>
It is generally best practice to not use the getPageSource() method of WebDriver, but to rather use JavaScriptExecutor to get the page source through javascript.
String pageSource = ((JavaScriptExecutor)driver).executeScript("return document.documentElement.outerHTML;").toString();
Yes, it's a problem for child elements. You can either use javascript as already told or url decode what you got and receive the initial source code.
String result = java.net.URLDecoder.decode(url, "UTF-8");
Related
How to choose cssSelector() from Chrome browser using Chrome Developer Tools?
Can you show an example for the below code?
WebElement searchBox = driver.findElement(By.cssSelector("selector"));
searchBox.click();
How to chose the value for the SELECTOR field from the Chrome Developer Tool?-F12 in Chrome. Please explain with an example
Thanks
If you are referring to Selenium like your tags imply, I guess you're trying to use
driver.findElements(By.className(".."));
or something similar, you need to be familiar with CSS Selectors.
for example if we inspect the stackoverflow logo we will see:
<div id="hlogo">
<a href="/">
Stack Overflow
</a>
</div>
So in this case we will use:
driver.findElements(By.id("hlogo"));
Or we can use a cssSelector by doing:
driver.findElements(By.cssSelector("#hlogo"));
If i understand you question properly, thats what you need:
$$("your_selector")
For example:
$$("#id_of_element")
$$("[type="text"]")
I am trying to parse Octane benchmark page http://octane-benchmark.googlecode.com/svn/latest/index.html , with WebElements:
<div class="hero-unit" id="inside-anchor">
<h1 id="main-banner" align="center">Start Octane 2.0</h1>
<div id="bar-appendix"></div>
</div>
I've started Selenium WebDriver on my tablet device (using Java, Eclipse, Selendoroid)
SelendroidConfiguration config = new SelendroidConfiguration();
selendroidServer = new SelendroidLauncher(config);
selendroidServer.lauchSelendroid();
DesiredCapabilities caps = SelendroidCapabilities.android();
driver = new SelendroidDriver(caps);
and I've initialized driver with Octane page:
driver.get("http://octane-benchmark.googlecode.com/svn/latest/index.html");
I am trying to parse it with xpath:
String xpathString = "//div[#class='hero-unit']//h1";
String line = driver.findElement(By.xpath(xpathString)).getText();
System.out.println(line);
but Java returns NullPointer Exception (on line)- function FindElement() can not find anything on this .html page.
Driver is started well, it returns appropriate value for getCurrentUrl() function, but can not return PageSource(), and can not return any value for findElement(By.something...).
Looks like, this Octane page has something that stops every search request (during parsing process). On the same way I have parsed 7 other benchmark pages, and they worked well, but this Octane page...acts just like it is "empty" for WebDriver...
I don't know is it because of
<script type="text/javascript">
part, or something else?
Is this Octane benchmark page special about something?
Thanks...
xPath() works with sites that conform to XML standard.HTML is more forgiving; you can have missing end tags and other errors but in XML this is forbidden. So chances were that the html does not conform with the XML standard so I double checked by validating your link at this site:
http://www.w3schools.com/xml/xml_validator.asp
And guess what? It had some errors. You save yourself the trouble next time by validating on this site first. Of course, that doesn't mean that XML conforming sites are all suitable for xPath() webscraping(hidden elements, javascript, etc.). However, from the nature of the reported error you might be able to tell which is not.
The By.xpath() only works if the html page conforms to XML standards. Probably the Octane 2.0 page does not comply and hence the method returns null.
I'm trying to build tests for an old system. The HTML is not well formed. I need to identify and click a radio button.
The html looks like this:
...
<td class="tablerow" colspan="3">
<INPUT type=radio name="ticket" value="22" >ramdom1
<INPUT type=radio name="ticket" value="1" >ramdom2
<INPUT type=radio name="ticket" value="3" >ramdom3
<INPUT type=radio name="ticket" value="99" >ramdom4
</td>
...
I was trying to select the input using xpath as follows:
String xpath = "//input[contains(#name, 'ticket') and contains(#value, '3')]";
WebElement rb = driver.findElement(By.xpath(xpath));
But selenium doesn't found the element.
If change it to
String xpath = "//input[contains(#name, 'ticket')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
or
String xpath = "//input[contains(#value, '3')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
It works, selenium returns a list of elements, including the one that I need. The problem occurs only when I try to use both conditions in a same xpath.
Of course that I could iterate over the list and test each value, but I would like to understand if I'm doing something wrong or not. Since IE doesn´t have native xpath support, I thought this could be a selenium implementation issue.
I'm using Selenium WebDriver (2.37.1) with IE Driver.
Not sure whether this is a Selenium implementation issue but this should work:
"//input[contains(#name, 'ticket')][contains(#value, '3')]"
The use of and is basically the same so the result should be correct here.
I am unsure why that doesn't work, and this technically isn't an answer, but you can replicate precisely what Selenium does to ensure it's not Selenium or any of it's tools at fault.
Selenium uses a library called "Wicked Good XPath" for a Javascript-based implementation of an XPath engine because IE doesn't have a "native" one.
So, to reproduce the scenario, take a copy of your page and add Wicked Good XPath to the script headers. Documentation on the front page of that website is very simple, and very easy to follow.
Once loaded in IE, open the Developer Tools and go into the Console. Wicked Good XPath will need to be "initialised" as such, and therefore you'll need to call wgxpath.install() in the console.
Once done, you now have access to the same library that Selenium would be using. Now, you can call a function within IE's developer console to access the DOM using XPath:
document.evaluate("//input[contains(#name, 'ticket') and contains(#value, '3')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
The exact element you need will be returned, at least for me.
Now, admittedly, you don't need XPath at all for this, you can get away with using CSS selectors:
input[name~='ticket'][value='3']
We can use the following css
1.css=input[value='22']
2.css=input[value='1']
3.css=input[value='3']
4.css=input[value='99']
For Checking the Radio Buttons.
Selenium-2.37.0
Firefox 24.0 (although have also tried on Chrome)
Mac OS X Mountain Lion 10.8.5
Solutions involving Windows, Windows-specific automation tools/libraries or O/S other than Mac OS X are not acceptable answers
Firstly may I remark that very similar questions have be asked often here on Stackoverflow, I have examined every offered answer, and none of them work for the CuteWebUI_Uploader_Resource AJAX file uploader, as I will illustrate in detail below.
I have an Enterprise Java web application with data and files and wish to automate mapping and uploading a selected subset of the data and files from that web application against (into) a separate ASP.NET web application, which uses the CuteWebUI toolkit and the CuteWebUI_Uploader_Resource from CuteSoft.
At the time of writing the http://cutesoft.net server has been down for some days; Googling gives some CuteSoft forum hits that may help with this problem, but I currently can't access them.
Like many file uploaders, the CuteWebUI AJAX file uploader has a Browse button and an Upload button.
I want to be able to upload directly into the CuteWebUI AJAX file uploader without using that Browse button (and without having to play any tricks using the Browse dialog on Mac OS X, because the files I wish to upload will not be available to a browser on a local filesystem, but instead are stored on a server accessible to the Java web application via Java, although for testing I can have them on a local filesystem, as shown below).
I appreciate that this problem has been answered many times for other file uploaders (see exhaustive list at end of this posting), and the process usually described as a solution for most file uploaders is roughly:
Identify the INPUT field for the file upload path (sometimes hidden) populated by the Browse button. Sometimes one needs to unhide this using a JavascriptExecutor and ensure it is a text field, then use sendKeys to populate it.
Identify the matching SUBMIT Upload button and click() it.
This does not work for the CuteWebUI AJAX file uploader.
The system I am targeting does indeed have an INPUT element that by name would suggest it is used to store the file upload path, but in fact when I try it by hand (and examine it using Firebug and Selenium IDE) its value is not populated at all, and instead a completely new temporary table of uploadable files appears.
The following HTML shows the situation before any file browse or upload has been attempted. I have omitted some style markup and some irrelevant values:
<script src="/CuteWebUI_Uploader_Resource.axd?type=script&_ver=" type="text/javascript"></script>
<input
type="hidden"
autocomplete="off"
name="fileuploader_433"
id="fileuploader_433"
isuploaderfield="1"
value="">
<button>Browse</button>
<span style="display: none;"></span>
<button style="display: none;">Cancel upload</button>
<img
showprogressbar="1"
canceluploadmsg="Cancel upload"
resourcehandler="/CuteWebUI_Uploader_Resource.axd"
uploadtype="Auto"
cancelallmsg="Cancel all Uploads"
uploadurl="/Handlers/UploadHandler.ashx"
insertext="Upload a file"
uploadingmsg="Uploading.." filetoolargemsg=".." maxfileslimitmsg=".."
inserttext="Browse"
numfilesshowcancelall="2147483647"
barstyle="Continuous"
showprogressinfo="0"
multiplefilesupload="0"
windowsdialoglimitmsg=".."
manualstartupload="1"
extensions=""
contextvalue=".."
onerror="this.onload()"
onload="this.style.display="none" ;
if(!window.CuteWebUI_AjaxUploader_Initialize){
var xh=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHttp');
xh.open('GET','/CuteWebUI_Uploader_Resource.axd?type=script&_ver=',false);xh.send('');eval(xh.responseText)}CuteWebUI_AjaxUploader_Initialize(this.id);"
src="/CuteWebUI_Uploader_Resource.axd?type=file&file=continuous.gif"
pageupload="1"
namespace="CuteWebUI"
uniqueid="fileuploader_433"
id="fileuploader_433_Loader_unique"
style="display: none;">
<button
onclick="return submitbutton_click('433')"
class="submitbutton"
name="SubmitButton"
id="SubmitButton">Upload</button>
The following Selenium in Java correctly unhides and populates the fileuploader_433 INPUT field, but on executing the click() on the submit I still get a popup window asserting I have to use the browse button with the message "Please use browse for file upload":
String name_fileuploader = "fileuploader_434";
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementsByName('" + name_fileuploader + "')[0].setAttribute('type', 'text');");
WebElement element_uploader = driver.findElement(By.xpath("//input[#name='" + name_fileuploader + "']"));
element_uploader.clear();
String filepath="/path/to/file.pdf";
element_uploader.sendKeys(filepath);
driver.findElement(By.xpath("(//button[#id='SubmitButton'])[2]")).click();
Firebug confirms that even if you do it by hand, that INPUT field is never used ! Instead, a new file queue table appears:
<script src="/CuteWebUI_Uploader_Resource.axd?type=script&_ver=" type="text/javascript"></script>
<input type="hidden" autocomplete="off" name="fileuploader_434" id="fileuploader_434" isuploaderfield="1" value="">
<button>Browse</button>
<table .. class="AjaxUploaderQueueTable">
<tbody>
<tr class="AjaxUploaderQueueTableRow">
<td>
<img src="/CuteWebUI_Uploader_Resource.axd?type=file&file=circle.png&_ver=null" title="">
</td>
<td>file.pdf</td><td>
<img src="/CuteWebUI_Uploader_Resource.axd?type=file&file=stop.png&_ver=null" title="Remove" style="cursor: pointer;">
</td>
</tr>
</tbody>
</table>
<span style="display: none;"></span>
<button style="display: none;">Cancel upload</button>
<img showprogressbar="1" canceluploadmsg="Cancel upload" ..
resourcehandler="/CuteWebUI_Uploader_Resource.axd" uploadtype="Auto"
cancelallmsg="Cancel all Uploads" uploadurl="/Handlers/UploadHandler.ashx" [snip]
onerror="this.onload()" onload="this.style.display="none" ;
if(!window.CuteWebUI_AjaxUploader_Initialize){var xh=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHttp');xh.open('GET','/CuteWebUI_Uploader_Resource.axd?type=script&_ver=',false);xh.send('');eval(xh.responseText)}CuteWebUI_AjaxUploader_Initialize(this.id);"
src="/CuteWebUI_Uploader_Resource.axd?type=file&file=continuous.gif"
pageupload="1" namespace="CuteWebUI" uniqueid="fileuploader_434"
id="fileuploader_434_Loader_unique" style="display: none;">
<button onclick="return submitbutton_click('434')" class="submitbutton" name="SubmitButton" id="SubmitButton">Upload</button>
Please note that:
the hidden INPUT element is never populated with a file path value !
instead, the string file.pdf without the full file path appears in a new TABLE with class="AjaxUploaderQueueTable" below the Browse button.
That inserted AjaxUploaderQueueTable is clearly only cosmetic; it seems that the filepath selected during the Browse operation is stored server-side. I have searched for the full filepath after selecting with the Browse button using Firebug and it is not present in the page.
I am open - for the sake of testing only - to suggestions that instead involve tricks manipulating the Browse dialog in Mac OS X, but that approach does not meet my eventual requirements, because I can't rely on that approach for the final task of uploading many files from an Enterprise Java web application into the targeted ASP.NET web application. I must side-step the Browse dialog completely.
Background research
All of the following have related questions and answers but none of these solve the problem for the CuteWebUI AJAX file uploader:
How to type some text in hidden field in Selenium WebDriver using Java
How to handle windows file upload using Selenium WebDriver?
How to force Selenium WebDriver to click on element which is not currently visible?
how does selenium webdriver upload files to the browser?
How to deal with file uploading in test automation using selenium or webdriver
https://stackoverflow.com/questions/18886970/selenium-webdriver-upload-file-sendkeys-dont-work
File Upload using Selenium WebDriver and Java
How to handle with uploading files from modal window Selenium WebDriver Java
Handling a popup window using selenium
http://selenium.10932.n7.nabble.com/Handling-browser-pop-up-dialog-to-upload-a-file-td29153.html
This probably relevant forum link from CuteSoft is down:
Jul 24, 2012 - Hi,I'm doing automated testing for my web system using selenium webdriver, the problem is that I can't interact with ajaxuploader component
And a WebDriver forum posting with the INPUT unhide trick: https://groups.google.com/forum/#!topic/webdriver/JAXC_qEbQvI
I've found a solution for this; it comes with some caveats but it works for my situation. Hopefully it will help you too.
The biggest caveat is that I only know that it works with the current version of AjaxUploader as downloaded today. I'd been using an older version that worked differently; I can't specify a version number since CuteSoft seem pathologically inclined to make the version numbers of AjaxUploader almost impossible to figure out. I can't find a version number for the current version OR the older one that I was using.
The other caveat is that it relies on there only being one AjaxUploader control on the page at a time. It appears that AjaxUploader DOES create an <input type='file'> element, but it's left dangling in a div at the top of the <body> tag, without any id or name attribute or anything else to link it to a particular uploader. I'm not sure what AjaxUploader does when more than one of it are present: maybe it creates multiple file inputs and tracks which is which by javascript; maybe it shares one. For my situation I didn't need to solve that part so I didn't try it.
Anyway, the trick is to find the file input as follows:
webDriver.findElements(By.cssSelector("body > div > input[type='file']")).get(0).sendKeys(fileName);
Note that the selector here has nothing to do with the ID or any other characteristics of the particular upload button you're using. It's just "the first file input inside a div directly inside the body tag". That's where AjaxUploader inserts its file input.
There appears to be no need to interact with any of the rest of the UI, including the Browse button: sendKeysing the filename to the right file input causes the upload to begin immediately. (This is an edit; I originally thought that clicking the button was necessary too but it doesn't seem to be)
Note that to make sure the timing of your test works correctly, you probably want to follow this click up with a wait operation that waits until the file has finished uploading (eg by waiting for the HTML that the AjaxUploader inserts after uploading is complete) before proceeding to whatever you want to do next.
I would like to add a functionality that shows a pdf on a certain page. I have the pdf as a File in my code. So I cannot get it from my webserver.
I also have the pagenumber.
I've seen that the adobe plugin accepts #page=1 but I don't know how to use this in a web based application.
I also found some tutorials wich display it in an Iframe. But these use :
org.apache.wicket.markup.html.DynamicWebResource
And this doesn't exist anymore.
I found it's replaced by
org.apache.wicket.request.resource.ByteArrayResource
Are there any tutorials/tips on how to do this (Wicket 1.5 or higher)?.
You can use ResourceLink with ByteArrayResource, or you can use any link/button and in its onClick/onSubmit() you can do:
getRequestCycle().scheduleRequestHandlerAfterCurrent(
new ResourceRequestHandler(new ByteArrayResource(...)))
In both cases you may need to set the content-disposition header by overriding org.apache.wicket.request.resource.ByteArrayResource#configureResponse() and calling ResourceResponse#setContentDisposition(INLINE) for it.
When you modify the data from you're object tag pass a pagenumber. This passes the page to the adobe plugin and automatically sets the page. (I have only tested this in chrome 21 and IE9)
<object wicket:id="myFile" data="" border="1" width="100%" height="600px"></object>
WebMarkupContainer wmc = new WebMarkupContainer("myFile");
wmc.add(new AttributeModifier("data", (String) urlFor(rr, null)+"#page="+ pageNmbr));
add(wmc);