How set content frame using htmlunit - java

I need to set the content HTML within an iframe, as pictured.
1- Below, I get the frame. But the frame does not have a setContent
FrameWindow frame = page.getFrames().get(0);
2- So I got BaseFrameElement. But, how to put html code in content?
BaseFrameElement frame = page.getFrames().get(0).getFrameElement();
frame.setTextContent(textContent);
If you have a better solution, let me know.
Thank you.

Not sure that i understand your problem. Usually some action on the page modifies the content of the iframe - there is no need to do this from the 'outside'.
Try something like
BaseFrameElement base = page.getFrames().get(0);
base.getEnclosedWindow().setEnclosedPage(newPage);
Keep in mind that this will not trigger all the events and might not have the result you expect.

Related

Unable to locate the element when Page factory is used for webelements

Here i am unable to click the iframe href using the Page factory but if i try seperately without pagefactory i am unable to click the Href
DOMImage: enter image description here
#FindBy(xpath="//*[#id="content"]/div/ul/li[2]/a")
WebElement iFramelabeltext;
Tried various approachs:
//a[normalize-space()='iFrame']
//a[#href='/iframe']
Need help in solving this ?
Firstly, you need to understand what is a Frame actually - it's a html code block inside entire html page code. So Selenium cannot directly access it.
You need to switch to that Frame and then perform needed actions.
How to deal with frames, see here - https://www.guru99.com/handling-iframes-selenium.html

Java Selenium Get Element for Click

i have some problems to get the right element to perform a click.
I use selenium.
I want to click on the Log In "Button" on this page https://campus.uni-stuttgart.de/cusonline/webnav.ini
Maybe sb could help me.
Thanks
Try this code. The login button is available under a frame. so it is necessary to switch to that frame in order to access the element.
driver.get("https://campus.uni-stuttgart.de/cusonline/webnav.ini");
driver.switchTo().frame(driver.findElement(By.xpath("//*[#name='menue']")));
driver.findElement(By.cssSelector("#menue_frame_key_icon > img")).click();
driver.switchTo().defaultContent();
It appear the login button id is "menue_frame_key_icon".
So this should be:
driver.findElement(By.id("menue_frame_key_icon")).click();
You could alternately try the child element of that id, which can be done multiple ways. This will work though:
driver.findElement(By.cssSelector("#menue_frame_key_icon > img")).click();

Modifying css property of an element which is fixed at the top using javascript

I'm trying to take multiple screenshots in java selenium webdriver . Taking a site such as mashable.com , there is a static header at the top. When I take my first screenshot there is no problem but as I scroll down to take a screenshot, there is a header on top which blocks some content.
Now, I don't wish to have the header on top. Manually, I played around with the css of the site in chrome. I found that by identifying the id and setting the position from position from fixed to null , I get the header removed. Is it possible to have a general way to identify these headers (only on the top) and modify their css property using javascript ?
You can try this:
$('*').filter(function() {
if ($(this).css('position') === 'fixed'){
$(this).css('position', 'relative');
}
});

How to update some components of my JSP page using JavaScript

I have a Jsp page and I want automaticly update one of the form on it using Js. Can somebody suggest something
Thanks)
You can dynamically update the elements of the form using basic JavaScript, if thats what you are looking for. Here are some dirty examples:
Eg.
If the id of your form is myForm, you can use
document.forms["myform"].action = "somepage"; //to change the action
var elem1 = document.getElementById("elementID")` //to get an element
var elem2 = document.forms["myform"].element //other way to get an element
childElement = document.createElement("option"); //to create a new element
myform.appendChild(childElement); //to append some child-element to the form
etc. The values/attributes/styles can be changed for the elements too using simple JavaScript. Any JavaScript tutorial on the internet should be helpful.
Have a look at the jQuery library (http://jquery.com/), that will allow you to manipulate HTML elements on the page, including the form element and it's children to automatically update them however you please :)
For an update from server side i.e Data from database or some other file on server use ajax
Now it depends on you to go for javascript or jquery to make this work.
Google this you can find good solutions.

make document available for download through java/servlet

I need to know if there is a way in java/servlet to make documents(doc,pdf) stored in database available for download to users in requested way(please see below),
for example there is a web page and the link for document in it
right now it is done this way:
if the user clicks that link than a new blank window opens and there the download dialog box is shown and the user is able to download the document but that blank window stays open
and the user have to close it manually
but wish to do it this way:
If the User clicks that link than directly staying on that page a download dialog box should show up asking them to save the file
a servlet url handles the download of the document which is responsible for extracting the doc form database and makes available for download to users
thank you for your time and effort
You need to add following headers in your servlet to make it a downloadable content so browsers don't try to display it,
String value = "attachment;filename=\"" + URLEncoder.encode(filename, "UTF-8") +'"';
response.setHeader("Content-Disposition", value);
response.setHeader("Content-Transfer-Encoding", "binary");
The filename is proposed filename and user can change it.
I wonder if your link html doesn't have something like:
<a href="/foo" **target="_blank"** ....>download</href>
Otherwise, it should work as you want.
This is a bug in IE which depends on several things, the content type is one of them. We had the same problem a few years ago but I don't remember the correct solution anymore, only that we struggled with this for quite some time. Try this:
Use the correct content type (application/pdf)
If that doesn't work, use a wrong file type (like application/octet-stream) which should tell IE to leave the file alone. You may have problems with the file extension, though.
Send or don't send the correct file size
Check which chunking mode you're using.
One of these things made IE behave. Good luck.
You need to remove target="_blank" from your <a> element.
Edit: as per your comments: you need to set Content-Disposition header to attachment. You can find here examples of a simple fileservlet and an advanced fileservlet to get some insights.

Categories

Resources