Htmlunit - click() doesn't work - java

I am spidering a web page ... but click() doesnt work, or doesn't navigate ...
Any clue what could be the issue ? I'm using 2.25 version
you can see all details in the code, thank you very much in advance.
this my code:
#Test
public String xxx() throws Exception {
try (final WebClient webClient = new WebClient(BrowserVersion.getDefault())) {
webClient.setJavaScriptTimeout(15000);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.waitForBackgroundJavaScript(30000);
webClient.getOptions().setActiveXNative(true);
webClient.getOptions().setAppletEnabled(true);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setRedirectEnabled(true);
// add to log variable
//toLog.append(" contrato: ").append(numeroContrato);
final HtmlPage consultaCuentaPage = webClient.getPage(url);
// grab first form
final HtmlForm consultarCuentaForm = consultaCuentaPage.getForms().get(0);
// numero cuenta input
final HtmlInput numeroCuentaInput = consultarCuentaForm.getInputByName("nroCuenta");
// consultar button
final HtmlInput consultarButton = consultarCuentaForm.getInputByName("commandConsultar");
// Change the value of the text field
numeroCuentaInput.setValueAttribute("1111");
// Now submit the form by clicking the button and get back the second page.
final HtmlPage consultaCuentaPage2 = consultarButton.click();
LOG.info("time: " + consultaCuentaPage2.getWebResponse().getLoadTime());
// LOG.info("time: " + consultaCuentaPage2.getWebResponse().getContentAsString());
//System.out.println(consultaCuentaPage2.asText());
////*[#id="form"]/div[1]/fieldset/table/tbody/tr[1]/td[1]
if (consultaCuentaPage2.getFirstByXPath("//*[#id=\"form\"]/div[1]/fieldset/table/tbody/tr[1]/td[1]") != null) {
return ((HtmlTableDataCell)consultaCuentaPage2.getFirstByXPath("//*[#id=\"form\"]/div[1]/fieldset/table/tbody/tr[1]/td[1]")).asText();
}
}
return null;
}

Related

I have tried to copy URL which is opened in new tab but copying old tab urls. How to copy new tab url java selenium?

How to copy new tab url java selenium?
'package TestCases;
public class Learn_TC3 extends SuperTestScript
{
#Test
public void LoginTC1() throws Exception
{
//all the required data
String USRID = ExcelLibrary.readData("Sheet1", 0, 0);
String PSW = ExcelLibrary.readData("Sheet1", 0, 1);
//create page objects
LearnPage Lp = new LearnPage();
Tabswitch Ts = new Tabswitch();
//invoke the methods
Lp.ClickonMaterialsButton();
Thread.sleep(3000);
Lp.ClickonPDF1();//By clicking on pdf. Pdf opens in new tab
String CurrentUrl = driver.getCurrentUrl();// to Fetch new url
ExcelLibrary.writeData("Sheet1", 0, 4, CurrentUrl);//write url to excel sheet?
Ts.switchToPreviousTabAndClose(); //Closing new tab
}
}'
I have tried to copy URL which is opened in new tab but copying old tab urls. How to copy new tab url java selenium?
After clicking on Lp.ClickonPDF1() that opens a new tab you have to switch Selenium driver to the opened tab in order to perform actions there.
So your code can be something like this:
Lp.ClickonMaterialsButton();
Thread.sleep(3000);
Lp.ClickonPDF1();//By clicking on pdf. Pdf opens in new tab
Thread.sleep(500);
List<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
String CurrentUrl = driver.getCurrentUrl();// to Fetch new url
ExcelLibrary.writeData("Sheet1", 0, 4, CurrentUrl);//write url to excel sheet?
Ts.switchToPreviousTabAndClose(); //Closing new tab
Not sure how can I use the driver object in your infrastructure, so I used it as regular, with no relation to any page object instance.
Below are methods that I created for a test project (using cucumber-java and selenium-java) that is similar to your approach, where it was required to:
Click on a link for a pdf document on a webpage, which opened in a new tab
List the opened tabs
Move to the new tab
Get the url for the pdf document and verify (assert) that it was correct
Close the pdf tab
Return to the webpage
Feel free to use what you need
#Then("^on \"([^\"]*)\" I can view the Pdf Document of \"([^\"]*)\"$")
public void on_I_can_view_the_Pdf_Document_of(String relativeUrl, String document) throws Throwable {
viewPDFDocument(relativeUrl, document);
}
private void viewPdfDocument(String relativeUrl, String document) throws Exception {
clickOnLink("", document);
verifyPdfUrl();
}
public void clickOnLink(String linkXpath, String label) throws Exception {
LOG.info("Clicking link '{}' with xpath '{}'", label, linkXpath);
retryIfAssertionFails(() -> {
String xpath = linkXpath + "//a/descendant-or-self::*[contains(normalize-space(text()), '" + label + "')]";
waitForElementToBeClickable(xpath);
driver.findElementByXPath(xpath).click();
return null;
});
LOG.info("Link clicked");
sleep(50);
}
private void verifyPdfUrl() throws Exception {
String parentWindowHandle = driver.getWindowHandle();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
for (String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(parentWindowHandle)) {
driver.switchTo().window(windowHandle);
}
}
String pdfTabUrl;
pdfTabUrl = driver.getCurrentUrl();
assertTrue(pdfTabUrl.contains(
"/app/restrict/application/caseInformation.pdf")); {
System.out.println("Correct url presented: " + pdfTabUrl);
}
if (!driver.getWindowHandle().equals(parentWindowHandle)) {
driver.close();
}
driver.switchTo().window(parentWindowHandle);
}
In your case it is updating to the following:
Lp.ClickonMaterialsButton();
Thread.sleep(3000);
Lp.ClickonPDF1();//By clicking on pdf. Pdf opens in new tab
String parentWindowHandle = driver.getWindowHandle();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
for (String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(parentWindowHandle)) {
driver.switchTo().window(windowHandle);
}
}
String pdfTabUrl;
pdfTabUrl = driver.getCurrentUrl(); // to Fetch new url
ExcelLibrary.writeData("Sheet1", 0, 4, pdfTabUrl); //write url to excel sheet?
if (!driver.getWindowHandle().equals(parentWindowHandle)) {
driver.close();
} //Closing new tab
driver.switchTo().window(parentWindowHandle); //Switch back to original tab
You can do that pretty easily with Selenium 4
Code:
driver.get("https://www.google.com");
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to("https://www.stackoverflow.com");
System.out.println(driver.getCurrentUrl());
Output:
https://stackoverflow.com/
You can do like below with your code,
Lp.ClickonPDF1();
driver.switchTo().newWindow(WindowType.TAB);
String CurrentUrl = driver.getCurrentUrl();

HtmlUnit wait for redirect

I'm having following scenario I need to implement in HtmlUnit:
-I click button
-Im getting redirected to page www.page.pl/result
-I need to wait few seconds and then I'm getting redirected to www.anotherpage.pl/auth_code=qwe123
The problem is, once I'm getting to /result page, I can't get out of it to next one. There are no manual actions there, I just need to wait until redirect happens
Here is my WebClient
private WebClient initWebClient() {
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
return webClient;
}
And how I'm trying to accomplish this, I'v tried several ways, including Thread.sleep() already...
HtmlPage page = webClient.getPage(url);
page.getForms().get(0).getButtonByName("submit").click(); // Redirects me to /result
webClient.waitForBackgroundJavaScript(5000);
webClient.waitForBackgroundJavaScriptStartingBefore(5000);
Thread.sleep(5000);
page.refresh(); //I'm still on /result, while I need to be on completely new page by now
In case anyone else have similar issue, I found solution
webClient.addWebWindowListener(new WebWindowListener() {
#Override
public void webWindowOpened(WebWindowEvent webWindowEvent) {
}
#Override
public void webWindowContentChanged(WebWindowEvent webWindowEvent) {
if (webWindowEvent.getNewPage() != null) {
// do what you need to do with webWindowEvent.getNewPage().getUrl();
}
}
#Override
public void webWindowClosed(WebWindowEvent webWindowEvent) {
}
});

Receiving error when Uploading File using HtmlUnit

I am currently attempting to automate the upload of a file to a specific site. I am able to successfully login to the site and navigate to the import page; however, when I attempt to import the file, I receive an error. My guess is that it is because I am simply a user and am not granted permissions to write to the server. However, if I perform the import manually, then it is successful. Normally there would be four stages to the import. However, after the first step you can see that an error occurred. My code is listed below along with the error I am receiving:
public static void main(String args[]) throws Exception {
login();
}
#SuppressWarnings("resource")
public static void login() throws Exception {
// Open the webclient using Internet Explorer (Chrome does not work).
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER);
// Get the first page
final HtmlPage page = webClient.getPage("http://telephone.qqest.com/phone/Login/Login.asp");
// Get the form that we are dealing with.
final HtmlForm loginForm = page.getFormByName("frmLogin");
final HtmlTextInput userName = loginForm.getInputByName("Login");
final com.gargoylesoftware.htmlunit.html.HtmlPasswordInput passWord = (com.gargoylesoftware.htmlunit.html.HtmlPasswordInput)
loginForm.getInputByName("Password");
final HtmlTextInput companyID = loginForm.getInputByName("Ident");
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getCookieManager().setCookiesEnabled(true);
//final HtmlInput login = loginForm.getInputByName("Login");
// Change the values of the text fields.
userName.setValueAttribute("xxxx");
passWord.setValueAttribute("xxxxx");
companyID.setValueAttribute("xxxxx");
//create a submit button - it doesn't work with 'input'
DomElement loginBtn = page.createElement("button");
loginBtn.setAttribute("type", "submit");
// append the button to the form
loginForm.appendChild(loginBtn);
// submit the form
loginBtn.click();
//navigate to page for import.
HtmlPage page3 = webClient.getPage("http://telephone.qqest.com/phone/Imports/Employee/Module.asp");
//populate the textfield with the specified file.
final HtmlForm importForm = page3.getFormByName("ImportForm");
final HtmlFileInput inputFile = importForm.getInputByName("UploadFile");
inputFile.setValueAttribute("C:\\Users\\thisFile.xls");
inputFile.click();
final HtmlSubmitInput importBtn = (HtmlSubmitInput)importForm.getInputByValue("Import");
try {
importBtn.fireEvent(Event.TYPE_INPUT);
}
catch (NullPointerException ex) {
System.err.println(ex);
}
HtmlPage page4 = webClient.getPage("http://telephone.qqest.com/phone/Imports/Employee/ImportFile.asp?FileType=application/vnd.ms-excel&FilePath=c%3A%5Cinetpub%5Cwwwroot%5Cphone%5CDownloads%5CImports%5C&FileName=thisFile.xls.asp");
System.out.println("Import Page: " + page4.asText());
}
The error I am receiving:
Stage 1 of 4: Upload File - Completed
Microsoft JET Database Engine error '80004005'
Cannot update. Database or object is read-only.
/phone/Imports/Employee/ImportFile.asp, line 155
I have figured out a solution and wanted to post it for those who might find it useful in the future. The problem was that I was simply trying to redirect to the import page by using the URL. Prior I had tried to submit the forms with an importBtn.click() statement; however, I needed to declare this statement as its own HtmlPage variable.
final HtmlSubmitInput importBtn = (HtmlSubmitInput)importForm.getInputByValue("Import");
HtmlPage page4 = importBtn.click();
// loops until the page changes.
while(page4 == page3) {
// The page hasn't changed.
Thread.sleep(500);
}
System.out.println("Import Page: " + page4.asText());

Htmlunit button won't submit?

public void actionVote() {
HtmlForm form = this.page.getForms().get(0);
HtmlInput button = form.getInputByValue("vote");
try {
button.click();
} catch (IOException e) {
e.printStackTrace();
}
}
When i do println with button.asText(), it gives me the correct value of the submit button, but when I do button.click, nothing happens, like it doesn't submit the form.
I can't get the button using HtmlButton because the submit button doesn't have any id or name.
I also can't make it HtmlButton from HtmlInput.
Why doesn't this submit? Wrong element?
Try this - getInputByName or getButtonByName
HtmlForm form = this.page.getForms().get(0);
HtmlInput button = form.getInputByName("vote");
Or you can also create a fake button:
HtmlElement fakeButton = page.createElement("button");
button.setAttribute("type", "submit");
// add the button to the form
form.appendChild(fakeButton );
fakeButton.click();
Can you try below code:
HtmlForm form = page.getForms().get(0);
HtmlElement input = form.getElementsByAttribute("input", "name", "vote").get(0);
page = input.click();

Problem in HtmlUnit API for Java (Headless Browser)?

I am using HtmlUnit headless browser to browse this webpage (you can see the webpage to have a better understanding of the problem).
I have set the select's value to "1"
by the following commands
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
try {
// Configuring the webClient
webClient.setJavaScriptEnabled(true);
webClient.setThrowExceptionOnScriptError(false);
webClient.setCssEnabled(true);
webClient.setUseInsecureSSL(true);
webClient.setRedirectEnabled(true);
webClient.setActiveXNative(true);
webClient.setAppletEnabled(true);
webClient.setPrintContentOnFailingStatusCode(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
// Adding listeners
webClient.addWebWindowListener(new com.gargoylesoftware.htmlunit.WebWindowListener() {
public void webWindowOpened(WebWindowEvent event) {
numberOfWebWindowOpened++;
System.out.println("Number of opened WebWindow: " + numberOfWebWindowOpened);
}
public void webWindowContentChanged(WebWindowEvent event) {
}
public void webWindowClosed(WebWindowEvent event) {
numberOfWebWindowClosed++;
System.out.println("Number of closed WebWindow: " + numberOfWebWindowClosed);
}
});
webClient.setWebConnection(new HttpWebConnection(webClient) {
public WebResponse getResponse(WebRequestSettings settings) throws IOException {
System.out.println(settings.getUrl());
return super.getResponse(settings);
}
});
CookieManager cm = new CookieManager();
webClient.setCookieManager(cm);
HtmlPage page = webClient.getPage("http://www.ticketmaster.com/event/0B004354D90759FD?artistid=1073053&majorcatid=10002&minorcatid=207");
HtmlSelect select = (HtmlSelect) page.getElementById("quantity_select");
select.setSelectedAttribute("1", true);
and then clicked on the following button
by the following commands
HtmlButtonInput button = (HtmlButtonInput) page.getElementById("find_tickets_button");
HtmlPage captchaPage = button.click();
Thread.sleep(60*1000);
System.out.println("======captcha page=======");
System.out.println(captchaPage.asXml());
but even after clicking on the button and waiting for 60 seconds through the Thread.sleep() method, I am getting the same HtmlPage.
But when I do the same thing through real browser then I get the page that contains CAPTCHA.
I think I am missing something in the htmlunit.
Q1. Why am I not getting the same page (that contains CAPTCHA) through htmlunit's browser?
The web form on that page requires the quantity_select drop-down to be filled in. You're attempting to do that in your code by assuming the drop-down is a select element. However, it's no longer a select element. Try using Firebug to inspect the drop-down and you'll see that JavaScript has replaced the select with a complex set of nested div elements.
If you figure out how to emulate each user click on the divs for that unusual drop-down then you should be able to submit the form.

Categories

Resources