I would like to calculate the height and width of the main content area of a blogpost server side. This is something that needs to be done for many different websites. But mostly Wordpress sites. I need the width and height off the blogpost for an algorithm as parameters.
I could ask the user for this info, but unfortunately the people who are using this website are not technical so they have no idea how to find this information (eg in a developer tool). If the numbers are incorrect the outcome of my algorithm is most likely incorrect.
First I tried if there was a way to do this with PHP, but was not able to find it. Then I thought about using Java and then use PHP to communicate with Java again. Couldn’t find anything either.
Next step could be to let a user “select” a div or area and calculate it from there, but this would not be ideal, because you would need the user input every time you would like to make a calculation. One other problem with this would also be how to find the main content div / element first?
If I am able to find the main content div server side automatically I could load the blogpost in an iframe and calculate the width and height with jquery. But how would you find the main content div?
Anyone who has a way to solve this problem? I am kind of open to any solution without the user input.
Not going to be easy to do this server side, as you're not rendering the page.
I would suggest looking at using JavaScript to retrieve the dimensions of the Div you want, and then sending these server side from your script or from a form.
You could automate the process by triggering your JS function on page load, or trigger it using a button pressed by the user, or whatever.
For reference, you can get div height and width as follows:
var divHeight = document.getElementById('myDiv').clientHeight;
var divWidth = document.getElementById('myDiv').clientWidth;
Hope this helps.
Related
I would like to know if there is any way to verify that a specific elment of a web page is in a given position.
For instance, suppose you have a requirement stating:
"The lamp button shall appear on the upper left side corner of the web page".
How can I verify that the button is in the right position automatically?
Firstly, I should ask the requirements engineer to translate the left position in coordinate and since I have been working with Selenium 2.0 JAVA API, I can use
the method "getLocation" of the element. Is the only way to verify the element position.
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getLocation%28%29
There are two ways to verify the location of an element.
getLocation() verifies the absolute location of an element. I don't recommend this, it can change all the time depending on loading speeds and the size of the window.
getCssValue() can, to an extent, verify the relative location of an element. Most modern websites do their layout via CSS. You can do a getCssValue("display") which might return block,grid, or other layouts. You can check the margin, border, or padding.
However, the above solution is also brittle, because a site can change their layout quite frequently. It also requires the developer of the selenium code to know quite a lot about CSS. Finally, it isn't fail-proof. I can check for 20 different things, and still a single change in the CSS can completely change the position of the element.
I am working on a project where I need the outer width of a div on the server side. I am using jsoup for it.
How can I get the outerwidth of a div in jsoup, as we have outerWidth() function in jQuery?
You can't get the outerwidth of a div on server side because the server simply doesn't know the width. The real size of a element is depending on how the client renders the HTML and CSS.
Think about style="width: 80%" on the topmost element. The real size is depending on the size of the browser-window.
I don't know nothing about the context of your question but if you really need the size of Elements on serverside and it's your own application you could let the client tell this information to the server.
If the question is just about getting the size of Elements with Java you could start a Browser with Selenium and check the dimension with WebElement.getSize().
I'm looking for a solution to this and I have searched the web for an answer with nothing. I need a scrolling message on my webpage that can be updated by the same website but on a different page. (e.g www.webpage.com <-- has the scrolling message on it, www.webpage.com/settings <-- has a form in which you can change/update the scrolling message)
I'm pretty sure this is achievable with Java, but my knowledge on Java is slim and all my efforts have come to a fail. I have managed to get a scrolling message on my webpage and I'm sure if I could read a text file (with the message in) then assign that to a 'var', I could make that scroll but I don't know how to do that.
If you know a completely different method of doing this I am happy to change mine.
Thanks in advance,
Tom.
I would recommend javascript instead of java, since java is not really conventional anymore and looks ugly too (and requires a special plugin).
It depends on how fast you want to update the scrolling text how you want to do this. I would say save the text to a .txt or database from settings, and then have your page get it.
You can then use php to read the value from the .txt file or database to display it on your website.
If you want it to be extremely up to date you can let javasript call a little .php file that reads the file and gives you the content.
To make your text scroll you don't really need anything other than html, like so:
<marquee behavior="scroll" direction="left">Your scrolling text goes here</marquee>
You can read more about that here: http://www.quackit.com/html/codes/scrolling_text.cfm
I know this question has been asked before on S.O. and other websites but I haven't found a definite answer -- most of them say its not easily done but I wanted to make sure that was the final verdict.
Here's my situation:
I'm testing a website that is using Highcharts (http://www.highcharts.com) using Selenium WebDriver (Java).
I basically want to grab the information that is displayed in a small tooltip pop-up that appears when you hover your mouse over each datapoint on the Highchart's line graph.
Looking at the web page's HTML code, I noticed there is <g class="highcharts-tooltip".... I also noticed that, as you move your mouse, the (X,Y) values in ...transform="translate(X,Y)"> change, which then changes the information displayed in the tool tip.
Knowing this, my approach would be to somehow grab all the (X,Y) values and plug them into the transform fields and grab the tooltip data. But I don't know how to programmatically grab all the (X,Y) values through Selenium.
Has anyone tackles this issue in the past or has a better way to grab the necessary information?
I started creating a library to work with HighCharts, what I currently have is available here:
https://github.com/Ardesco/Powder-Monkey/tree/master/src/main/java/com/lazerycode/selenium/graphs
It's quite hard to provide a generic library that deals with HighCharts as the customisation options on the individual charts can modify the SVG markup quite a bit. Hopefully the above will help to a degree.
As I do more it will be updated.
The Line Charts have to have a background to work. If the chart is created with .setBackgroundColour(null), then the "rect" is not added to the html.
I was able to to add the background colour back in set to the body background colour. However I'll try and figure out a way of doing it without the "rect".
We have a form that lets people input html formatted text and that input is then displayed elsewhere on the webpage.
Due to layout constraints the input may not be longer than X lines. There is no preview of the edited text in the webpage. Input upon submit is immediately published. (Short Explanation why I cannot fix the layout: the text in question is displayed as an overlay above an image. The overlay has about the size of the image and that is fixed. The text should be completely visible inside that overlay and should not spill over.)
I am being asked to implement something to keep people from entering too much text.
My first try was going for "maxLength" but goes wrong because of the possibility html formatting of the input.
Besides the obvious two 1. expecting users to be smart and/or 2. implementing a preview method, how else could we possibly solve this?
I am out of ideas, I'll also accept an explanation why it is impossible
Technology used: java, wicket 1.4.x
I'm not a web-development expert so this may not be a precise enough answer.
Using javascript you can get the effective size of an HTML element once it has been rendered in the browser. Thus, one solution could be to render the page server-side, and check whether the result exceeds the size you expect.
This may not guarantee a correct result however because the server may render the page differently than the client. You could also always accept the input, then when the client renders the page, have a snippet of javascript that checks (client-side this time) whether the rendered result is okay. If it isn't, then let javascript redirect the client to an error page so that it can edit his input.
What I'd do however is to change the layout so that nothing breaks if the user enters too much text. Using the CSS overflow property could be a start. You could also implement better solutions in javascript such as dynamically changing the text size until it fits the size you want.