How to get the translated text from google translator using selenium? - java

I have tried the following code to get the google translator text using selenium:
result=driver.findElement(By.xpath("//body/div[#id='result_box']/span"));
I also tried these:
1.result=driver.findElement(By.xpath(".//body/div[#id='result_box']/span"));
2.result=driver.findElement(By.xpath("./*div[#id='result_box']/span"));
3.result=driver.findElement(By.xpath(".//div[#id='result_box']/span"));
4.result=driver.findElement(By.xpath("//div[#id='result_box']/span"));
5.result=driver.findElement(By.xpath(".//body/div[#id='result_box']/span"));
6.result=driver.findElement(By.xpath("./*[#id='result_box']/span"));
But none of the above works. I then tried to get the text by:
result=driver.findElement(By.id("result_box")).findElement(By.tagName("span"));
translatedtext=result.getText();
This returns a result but when I try to show the result in JTextarea it shows me '????' instead of the actual translated text.
I have also tried 'result.getAttribute("innerHTML")' but it also shows some question marks (?????) instead of the original translated text in JTextarea.
How can I solve this problem?

The result box has tag <span>, not <div>
result = driver.findElement(By.xpath(".//span[#id='result_box']/span"));
Or
result = driver.findElement(By.xpath(".//*[#id='result_box']/span"));
With double slash.

you also can use css selector like this:
result = driver.findElement(By.cssSelector("#result_box>span"));
people say that is faster than xpath

This worked for me, however i used python,you can try using equivalent of find_element_by_id function in Java
driver.find_element_by_id("gt-res-dir-ctr").text

Related

How to select text area in Selenium which has a HTML code as follows

<textarea rows="7" cols="125" name="nomText1Line1" required="required"></textarea>
I tried using driver.findElement(By.name("nomText1Line1")); didn't work
The way you mentioned is the easiest way to select your element. Are you sure there are no syntactical mistakes? This should work:
WebElement textArea = driver.findElement(By.name("nomText1Line1"));
If you are trying to get the contents of the text area then something like this:
String textArea = driver.findElement(By.name("nomText1Line1")).getText();

Selenium - PHP error - Finding text not contained in an tag

I find elements either by their ID or tag or etc. But my element is in a body tag with no tags at all, how can I find this? I know it is in the body tag but there are other elements too! The "text I want to find" is a php error displayed and I am hoping to catch that. I usually go writing WebElement x = driver.findElement(By.??); I cant proceed because I am uncertain what to do.
Sample HTML doc
<head></head>
<body>
Text I want to find
<div>xx</div>
<div>yy</div>
</body>
The reason for the java tag is, I am using Java to write my code?
In your situation I'd have used "context item expression" i.e. a .(dot) operator. So if I write an Xpath like this:
//div[contains(.,'Text To Be Searched')]
Then it will find all div elements which contain text Text To Be Searched. For you my answer would be
driver.findElement(By.xpath("//body[contains(.,'Text I want to find')]"));
You should add that text inside p tag and then you can write :
WebElement x = driver.getElementByTag('p');

getByXpath() not working inside frame

i am new to Htmlunit and trying to extract data from a website http://capitaline.com/new/index.asp. I have logged into the website successfully. When we log into website there are three frames.
One on the top to search for the company(like ACC ltd.) for which we are extracting data.
2nd frame has a tree which provide links to various data we want to look at.
3rd frame has the resulted data outcome on the basis of link you clicked in frame.
I managed to get the frame i need below:
HtmlPage companyAtGlanceTopWindow =(HtmlPage)companyAtGlanceLink.click().getEnclosingWindow().getTopWindow().getEnclosedPage();
HtmlPage companyAtGlanceFrame = (HtmlPage)companyAtGlanceTopWindow.getFrameByName("mid2").getEnclosedPage();
System.out.println(companyAtGlanceFrame.toString()); // This line returns the frame URL as i can see in my browser.
Output of print statement is
HtmlPage(http://capitaline.com/user/companyatglance.asp?id=CGO&cocode=6)#1194282974
Now i want my code to navigate down to the table inside this frame and for that i am using getByXPath() but it gives me nullPointerException. Here is the code for that.
HtmlTable companyGlanceTable1 = companyAtGlanceFrame.getFirstByXPath("/html/body/table[4]/tbody/tr/td/table/tbody/tr/td[1]/table");
My XPath for the current webpage(after i clicked the link)from which i am trying to extract table is seems correct, as it is copied from chrome element inspect. Please suggest some way to extract the table. I have done this type of extraction before but there i had id of table so, i used it.
Here is the HTML code for the table in the webpage.
<table width="100%" class = "tablelines" border = "0" >
I want to know that can you see the inner contents of each iframes in console (print asXml()), are they nested iframes?
well try this
List<WebWindow> windows = webClient.getWebWindows();
for(WebWindow w : windows){
HtmlPage hpage = (HtmlPage) w.getEnclosedPage();
System.out.println(hpage.asXml());
}
once you can see the contents,
HtmlPage hpage = (HtmlPage)webClient.getWebWindowByName(some_name).getEnclosedPage();
then using xpath grab your table contents(make sure your xpath is correct). It will work.(worked for me)
Thank you RDD for your feedback.
I solved the problem. Actually issue was not with the frame but with the XPath provided by chrome.
XPath Provided by chrome is:
/html/body/**table[4]**/tbody/tr/td/table/tbody/tr/td[1]/table
But the XPath worked for me is:
/html/body/**table[3]**/tbody/tr/td/table/tbody/tr/td[1]/table
It seems as, XPath provided by chrome has some glitch when there is a table within the path(Or may be some bug in htmlunit itself). I did many experiments and found that chrome always gives ../../table[row+1]/.. as XPath, while working XPath for htmlunit is ../../table[row]/..
SO, this code is working fine for me
HtmlTable companyGlanceTable1 = companyAtGlanceFrame.getFirstByXPath("/html/body/table[3]/tbody/tr/td/table/tbody/tr/td[1]/table");

How to Append java codes in JSP using JQuery

I am working with JSP pages and I need to .append() into a DIV element some java codes.
$("#myDiv").append("<% out.println("ali"); %>");
The previous code is actually wrong because there's quotes, so I escaped them.
$("#myDiv").append("<% out.println(\"ali\"); %>");
But I wasn't successful, nothing was appended to #myDiv
This is correct way to append. If nothing is appended, then the div might not exists in the page.
Try using firebug and check what6 is the output of console.log($('#muDiv'))
Your code should work, it sounds that jQuery is not loaded correctly or there is another problem.
http://jsfiddle.net/5PTwN/1/
Try this
$("#myDiv").append('<% out.print("ali"); %>');
below line also should work because java code will execute server side and inside append will be replaced with "ali".
$("#myDiv").append("<% out.print("ali"); %>");

Android get text from html

I get a special html code:
&lt ;p &gt ;This is &lt ;a href=&quot ;http://www.test.hu&quot ;&gt ;a test link&lt ;/a&gt ; and this is &amp ;nbsp;a sample text with special char: &amp ;#233;va &lt ;/p&gt ;
(There isn't space before ; char, but if I don't insert space the stackoverflow format it)
It's not a normally html code, but if I paste in a empty html page, the browser show it with normal tags:
<i><_p_>This is <_a_ href="http://www.test.hu">a test link<_/a_> and this is a sample text with special char: éva <_/p_>
</i>
This code will be shown in a browser:
This is a test link And this is a sample text with special char: éva
So I want to get this text, but I can't use Html.fromHtml, because the component what I use doesn't support Spanned. I wanted to try StringEscapeUtils, but I couldn't import it.
How can I replace special chars and remove tags?
I guess I am too late to answer Robertoq's question, but I am sure many other guys are still struggeling with this issue, I was one of them.
Anyway, the easiest way I found is this:
In strings.xml, add your html code inside CDATA, and then in the activity retrieve the string and load it in WebView, here is the example:
in strings.xml:
<string name="st1"><![CDATA[<p>This is a test link and this is a sample text with special char: éva </p>]]>
</string>
you may wish to replace é with &eacute ; (note: there is no space between &eacute and the ; )
Now, in your activity, create WebView and load string st1 to it:
WebView mWebview = (WebView)findViewById(R.id.*WebViewControlID*);
mWebview.loadDataWithBaseURL(null, getString(R.string.st1), "text/html", "utf-8", null);
And horraaa, it should work correctly. If you find this post useful I will be greatful if you can mark it as answered, so we help other struggling with this issue
Write a parser, no different than you would in any other situation where you have to parse data.
Now, if you can get it as ordinary unescaped HTML, there are a variety of open source Java HTML parsers out there that you can use. If you are going to work with the escaped HTML as you have in your first example, you will have to write the parser yourself.

Categories