I'm attempting to press a Javascript Button on a webpage using HTMLUnit 2.36 which navigates to another page, and so on...:
ScriptResult result = page.executeJavaScript("__doPostBack('LinkBtn_thebutton','')");
Page page = result.getNewPage();
I've attempted to use the code above which causes the following error, supposedly because getNewPage() is no longer supported:
The method GetNewPage() is undefined for type ScriptResult
I've also attempted to add a cast with getJavaScriptResult() as shown below with no luck:
HtmlPage page1 = (HtmlPage) result.getJavaScriptResult();
Causing the following error:
Exception in thread "main" java.lang.ClassCastException: class net.sourceforge.htmlunit.corejs.javascript.Undefined cannot be cast to class com.gargoylesoftware.htmlunit.html.HtmlPage
You are not supposed to cast the result.getJavaScriptResult();, treat it like a void. If your page is going to be redirected, make sure that redirecting is enabled: webClient.getOptions().setRedirectEnabled(true);
Related
Background
I am using htmlUnit to simulate user behavior in a certain page
I am reaching to a login page which I need to enter the user credentials
Issue:
The form that I am suppose to fill in the details dynamically changes and pushes new input fields with value that changes upon each char inserted.
This input field has several event listeners which as far as I was able to find from Chrome debugging the keypress event is the most relevant to me as this what ultimately generates the updates value
I am getting the following errors when the page "loads":
[User1st] An error occurred while extracting lang code TypeError: Cannot call method "getAttribute" of undefined
4.c.g.h.javascript.StrictErrorReporter : runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: *:x).] sourceName= https://???/jquery-1.10.2.min.js] line=[3] lineSource=[null] lineOffset=[0]
some code:
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.waitForBackgroundJavaScript(5000);
final HtmlPage page = webClient.getPage(WEBSITE_URL);
HtmlForm loginForm = page.getFormByName("login");
HtmlTextInput userIdField = loginForm.getInputByName("USERID");
HtmlPasswordInput passwordField = loginForm.getInputByName("USERPASSWORD");
userIdField.type("ID");
passwordField.setText("PASSWORD");
What I am doing next is simply iterating the form input fields and see their value.
How can I make sure that all related js code really getting executed if any?
I'm not sure if this helps but just letting the script sleeps work for me. This probably gives time loading all the js scripts.
Thread.sleep(2000);
I am trying to write some UI test cases for an SAP-webUI (web based) application. After login, it shows the dashboard ( Workcenter's ) screen.
Now the problem is, I am able to open the page, enter U/N, Pwd and login through Selenium. After i press the "Login" button the URL changes and the page got redirected/refreshed.
E.g. URL before login : https://a/b/c/d/e/f/g.htm?sap-client=001&sap-sessioncmd=open
E.g. URL after successful Login : https://a/b(bDsdfsdsf1lg==)/c/d/e/f/g.htm
After this im unable to perform any action or press any link in any part of the page. I tried with all the possible attributes ( css, xpath, id ). Webdriver was unable to find any element on the page. It shows the error "No element found" alone.
I am using java with Selenium Web Driver.
Please find the html structure of the webpage below
<html><body><div><div><iframe>#document<html><head></head><frameset><frameset><frame>#document<html><head></head><body><form><div><div><table><tbody><tr><td><div><ul><li><a id=abcdef></a></li></ul></div></td></tr></tbody></table></div></div></form></body></html></frame></frameset></frameset></html></iframe></div></div></body></html>
Actually i want to click a linkmenu "abcd", which is inside iframe and frame as shown in the below HTML code
<html><head></head><body><iframe name=a1><html><head></head><frameset><frameset name=fs1><frame name=f1><html><head></head><body><table><tbody><tr><td><ul><li><a id=abcdef>
I tried the below code as well.
driver.switchTo().frame("a1");
driver.findElement(By.id("abcd")).click();
OR
driver.findElement(By.xpath("//*[#id='abcd']")).click();
After using the above code, still im getting the error "No such element"
Kindly suggest
Regards,
Siva
Do it this way...
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#name='a1']"))); // switching to iframe
followed by
driver.switchTo().frame("f1"); // switch to frame
and then your desired action...
driver.findElement(By.id("abcd")).click();
This is because of the iframe. You need to switch to it first:
driver.switchTo().frame(0);
driver.findElement(By.id("abcdef")).click();
where 0 is a frame index.
See doc on implicit wait here
I guess you should do a implicit wait until your chosen element is available
modify these code to suit your chosen element:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Hey I'm having some trouble clicking a button to submit a comment on a thread using htmlUnit. I get the element by using this
HtmlPage newOne = webClient.getPage(string);
//System.out.println(newOne.asText());
HtmlTextArea area = (HtmlTextArea) newOne.getElementById("ctrl_message_html");
area.focus();
area.type('t');
newOne.getHtmlElementByAccessKey('s').click();
Which throws
com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 404 Not Found for http://forums.bukkit.org/threads/plugin-idea-to-get-tons-of-downloads.271185/members/jthort.90885864/post
at com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:514)
at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2067)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:717)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:804)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1322)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1265)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1213)
at HTMLUnit.main(HTMLUnit.java:87)
When I print out the element, I get this
HtmlSubmitInput[<input type="submit" class="button primary MenuCloser" value="Post" accesskey="s">]
But I don't understand why it would be throwing an exception when it clearly has the right object. I have javascript disabled, along with css. I am using the web browser Chrome.
Edit: By right Object I mean that it's the correct button on the page that I want to click and it's in the form of a HtmlSubmitInput button.
I'm not sure why you think having the right object will mean you won't get any exception. You're not getting a NullPointerException, if that is what you mean by the right object.
Having said that, if you read the exception it is a FailingHttpStatusCodeException with the following message:
404 Not Found for http://forums.bukkit.org/threads/plugin-idea-to-get-tons-of-downloads.271185/members/jthort.90885864/post
So that URL is giving you a 404 error when you perform an HTTP POST on it. You should keep debugging why this is happening. I would enable JavaScript, maybe it is needed there.
I'm trying to get the textbox with u_0_1e as id, from the page wall but HtmlUnit does not find anything. The last line prints null.
Here's the code:
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
WebClient client = new WebClient(BrowserVersion.CHROME);
JavaScriptEngine engine = new JavaScriptEngine(client);
client.setJavaScriptEngine(engine);
HtmlPage home = client.getPage("https://www.facebook.com/login.php");
HtmlSubmitInput login = (HtmlSubmitInput) home.getElementById("u_0_1");
HtmlTextInput name = (HtmlTextInput) home.getElementById("email");
HtmlPasswordInput pass = (HtmlPasswordInput) home.getElementById("pass");
name.setValueAttribute("myname");
pass.setValueAttribute("mypass");
HtmlPage page = login.click();
HtmlPage wall = client.getPage("https://www.facebook.com/");
System.out.println(wall.getElementById("u_0_1e"));
I have some comments about your issue.
First of all, you have disabled HtmlUnit's logging. So if you have any JavaScript issue then you are not going to see it. If you are actually getting a JavaScript error then the JavaScript code won't be fully executed. If the element you're trying to fetch was dynamically fetched from the server (probably using AJAX) then the JavaScript errors, if any, might result in that element not being fetched.
If you are webscraping, which is clearly the case, then you don't have any control over the JS so you can only accept it as not working or disable JS and manually processing the AJAX requests.
Of course, you will see the page perfectly working on a real browser but take into consideration that the JavaScript engine HtmlUnit uses is different from the real browsers.
Secondly, the two lines containing the word engine are absolutely unneeded.
Thirdly, as I mentioned in a previous question of yours, this will be more suitable to be handled by means of the Facebook API.
Finally, you might find this other answer useful:
JavaScript not being properly executed in HtmlUnit
I am trying to get all the main links, then click on them and navigation to the page:
WebClient client = new WebClient();
HtmlPage page = client.getPage(url);
// Get all links with a href of www.example.com/pages/1_
List<HtmlAnchor> links = (List<HtmlAnchor>) page.getByXPath("//a[href='www.example.com/pages/1_*'");
links[0].click();
After calling click, does it return a HtmlPage? (The NetBeans documentation is not telling me.)
Does the XPath expression look OK?
I don't know how documentation works in NetBeans but the documentation is all available online, and if you go to it, you'll see that the return type is <P extends Page> which will probably be HtmlPage most of the time, but could also be XmlPage or something like that.
Simulates clicking on this element,
returning the page in the window that
has the focus after the element has
been clicked. Note that the returned
page may or may not be the same as the
original page, depending on the type
of element being clicked, the presence
of JavaScript action listeners, etc.