Issue while clicking on 'find hotels' using Selenium-Java - java

I am using Selenium (web driver) - Java. I picked a travel site to do demo of automation.
On this travel site, at home webpage, I need to place following input before submit the form.
Provide City name
Date from and Date To
Click on Find hotel button.
I am able to do following:
Done
Done
Unable to click on Find hotel button.
On 3rd step, whenever I am clicking on 'Find button' through selenium code, it redirect page to some other website (not sure from where its redirection is coming).
My question is: is I am doing something wrong? Below are the details:
Web page source code:
<div class="clear"></div>
<div class='multiSearchBox' >
<div class='clear'></div>
</div>
<div class="block_bottom">
<div class="bottom">
<button class="search" type="submit">Find Hotels</button>
<input type="hidden" name="passengers" autocomplete="off" value="">
</div>
</div>
<div class="clear"></div>
</form>'
here is the button code which shows up in inspect element:
<button class="search" type="submit">Find Hotels</button>
Here is my code:
public void SubmitForm() {
WebElement Submit = Driver.findElement(By.className("search"));
System.out.println(Submit.getText());
Submit.submit();
}
I tried with following as well:
Submit.click();
But no luck.
However, I am getting button label: 'Find Hotels' in output but page gets re-directed to some other search page.
Doing manual click 'Find hotels' button on web page works fine.
I tried with Chrome and Firefox, I am facing same problem.
Please see if some can help.

You can use following xpath
//button[text() = 'Find Hotels']

Related

Click on submit button behaviour

Could someone please assist me in following:
I have button on several languages, which have to click on it. On EN it works correct, while on other languages when doing the same action, it behave as I am holding pressed left button on mouse and moving from left to right through button. It looks like as on screenshot below and HTML is also below.
HTML on DE page:
<div class="column large-12 text-center">
<input id="post-tip-submit" type="submit" class="button secondary expand" value="Veröffentlichen">
<div id="post-tip-loader-9" class="loader-large"><div>Loading...</div> </div><br><br>
</div>
And on EN page:
<div class="column large-12 text-center">
<input id="post-tip-submit" type="submit" class="button secondary expand" value="Publish">
<div id="post-tip-loader-9" class="loader-large"><div>Loading...</div></div><br><br>
</div>
It looks same except 'title' of button, but somehow click on EN button open next page, while click on other language button remains stuck on that page and button disappear (on both languages) after that 'click'.
One more thing: on EN page that submit button is much wider than on rest of languages (not sure how is important last statement).
Tried following code without success:
driver.findElement(By.cssSelector("#post-tip-submit")).isDisplayed();
driver.findElement(By.cssSelector("#post-tip-submit")).sendKeys(Keys.RETURN);
Thread.sleep(7000);
Also, this one:
driver.findElement(By.cssSelector("#post-tip-submit")).isDisplayed();
driver.findElement(By.cssSelector("#post-tip-submit")).click();
Thread.sleep(7000);
XPATH simply does not find that element, CSS does
Please, assist
an id is for an unique element. Try using a class, for example class="post-tip-submit" and cssSelector(".post-tip-submit"). Lets see if it works better?
Css selector is based on your markup hope it helps.
static Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(elementWaitTime, SECONDS)
.pollingEvery(2,SECONDS)
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.button[type=submit]")));
element.click()

Selenium cannot find valid xpath for the button click

Hello I'm learning selenium and I've met one problem. I was doing all things using xpath for buttons but with this one this doesn't work and I don't know why.
This is how looks button which I want to click (I want to click Order tickets button )
<div id="bookingOption" class="row top5" style="display: block;">
<div class="col-md-6">
<input name="bookButton" class="btn btn-primary" type="button" value="Order tickets">
</div>
</div>
My java code for clicking this button
I am using xPath //*[#id="bookingOption"]/div/input
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[ before #id='bookingOption']/div/input")));
driver.findElement(By.xpath("//*[ before #id='bookingOption']/div/input")).click();
Here is website on which on I am practising it may be helpful. http://ticketmonster-jdf.rhcloud.com/
I will be very thankful for every help.
Try:
//input[#value='Order tickets']
Remove before from xpath("//*[ before #id='bookingOption']/div/input")
the correct form for defining the xpath is
xpath("//*[#id='bookingOption']/div/input")

How to check for second element with class name and click it if it exists in Selenium Java

So I am writing automation tests using selenium and I am having a lot of trouble selecting the second element in a list of divs with the same class names
Boolean isExists2Accounts = driver.findElements(By.xpath("(//div[contains(#class, 'item-name')])[2]")).size() < 0;
if(isExists2Accounts)
{
//Finds second div element that has classname of item-name and clicks on it
driver.findElement(By.xpath("(//div[contains(#class, 'item-name')])[2]")).click();
}
else
{
driver.get("javascript:alert('There isn't a second account or you don't know how to select it!');");
Thread.sleep(5000);
org.testng.Assert.fail("transferTest6() Failed due to There isn't a second account or you don't know how to select it!");
}
HTML structure looks like this:
<div class="item-list">
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area</div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
</div>
Not really sure what I am doing wrong here, I looked at the html and there are 5 divs with the specified class name. Very new to selenium in general, using eclipse/junit/webdriver.
I have seen several questions similiar to this, and trying solutions people have posted have not worked. I have seen some suggestions to use .get(2) and I will try and implement that in the mean time.
Any help you could give would be good.
get(2) is THIRD element, not the second, as the countage begins from 0.
So:
driver.findElements(By.cssSelector(".item-name")).get(1).click();
OR depending on where is yr clickable
driver.findElements(By.cssSelector(".item-name div:not(.button-wrap)")).get(1).click();
Hey all the answer that was given by Stanjer works, I tested it with different markup, the developer that built the system I am testing through a random mousedown event (not click) for the html I am trying to interact with which was causing the problem.
So final solution with problem if it was a click event would be:
driver.findElements(By.cssSelector(".item-name")).get(1).click();
Just like he said.
However in this case I am instead going to send Javascript to the console to work with functions that have already been created by the developer.

Selenium Webdriver - Upload functionality not working due to Add button being invisible

Summary:
In Selenium Webdriver - Upload functionality is not working due to Add button being invisible.
Description:
The functionality is like, a pop up window opens for upload file, initially Add button is disable. The user needs to click on browse button, select the file and the system validates for the file format and size (4MB). If its valid then the browse text box is highlighted in green. After validation only, Add button gets enabled. On clicking Add, pop up closes and added material is displayed in main page.
Selenium Code and Error:
But below se code throws error with the error - org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with.
driver.findElementByXPath("//*[#id='fuVideo_ctl02']").sendKeys("D:\\Featured Materials");
Thread.sleep(1000);
driver.findElementByClassName("add_uploadbtn").click(); //Clicking on Add button
String Addtext = driver.findElementByClassName("add_uploadbtn").getText();
System.out.println("Add text" +Addtext);
Above Print Statement Result:
Add text
Note: While the script is running, document is uploaded and file validation starts and doesnt end..its showing for sometime and scripts ends with above mentioned error.
HTML:
Upload functionality code till Add button
<td colspan="2">
<span id="fuVideo" class="upload_file_txt mleft10" style="background: white" onclick="javascript:fnCheckFileIsUpload(this);">
<input id="fuVideo_ctl00" class="upload_file_txt" type="hidden" name="fuVideo$ctl00" style="width: 550px;" value="Copy of CAS QEA-PES Score Card Feb_2014_Capex Opex_STORM_Updated.xlsx">
<div id="fuVideo_ctl01" name="fuVideo_ctl01">
<input id="fuVideo_ctl02" class="upload_file_txt" type="file" style="width: 550px; background-color: Lime; color: black;" name="fuVideo$ctl02">
</div>
</span>
<br disabled="disabled">
<span style="margin-left: 25px;">Description</span>
<input id="txtDescriptionFU" class="add_edit_content_input" type="text" style="margin-left: 20px; width: 460px;" maxlength="500" name="txtDescriptionFU">
<input id="btnVideo" class="add_uploadbtn" type="button" onclick="javascript:fnFileUploadValidation();" value="Add">
Pls. help to solve this issue.
Thanks in Advance ....
1 second doesn't seem like enough time for the Add button to become visible. Instead of Thread.sleep(1000), use:
WebDriverWait wait = new WebDriverWait(driver, MAX_WAITTIME_IN_SECONDS);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("add_uploadbtn").click();
This assumes that driver.findElementByXPath("//*[#id='fuVideo_ctl02']").sendKeys("D:... is working properly.

Clicking <input type="image" /> using WebDriver, InternetExplorerDriver

I am trying to click on the image input type using webdriver, but it's not working.
The html code:
<input type="image" onclick="validationAndSubmit('next'); return false;" alt="Continue" src="/subscriptions/versions2/commonlab/images/buttons/continue.gif" name="continue">
The code I am using to click is:
driver.findElement(By.name("continue")).click();
I could invoke the Javascript like below, but that is not my intent.
((JavascriptExecutor)driver).executeScript("validationAndSubmit('next'); return false;");
I would like to simulate end user experience on IE as much as possible.
What should I do in order to click the image input?
thanks
Jenga

Categories