Click on submit button behaviour - java

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()

Related

Java and Selenium: Problems accessing checkbox elements in a modal

Page with a pop-up page/modal window, showing a list of (among others) checkboxes.
I'm having problems with Selenium not finding these elements.
Markup:
<td class="journalTabell-arkivstatus">
<div class="hb-grid hb-grid--gapSM">
<div data-e2e-selector="saksjournal-checkbox-style" style="visibility: visible;">
<input type="checkbox" data-e2e-selector="saksjournal-checkbox" id="erArkivert-0">
<div class="hb-label">
<label data-e2e-selector="saksjournal-checkbox-lbl" for="erArkivert-0"</label>
</div>
</div>
<div id="journalPostArkiverStatus-0">
<div class="haster ng-star-inserted">
<hb-ikon class="haster-ikon hb-ikon id="hb-alert-circle-98ec342a-966f-4f5f-9d84-24a2ac4c271e" aria-hidden="true">
<svg focusable="false">
<use xlink:href="assets/sprite.symbol.svg#ikon-hb-alert-circle"></use>
</svg>
</hb-ikon>
<div class="ng-star-inserted">Ikke arkivert</div>
<div class="ng-star-inserted">Last ned journalpostog kryss av når du har arkivert manuelt
</div>
</div>
</div>
</div>
</td>
There are several of these checkboxes, with id's erArkivert-0, -1, -2 and so on.
See the attached screenshot.
The Java/Selenium code looking for the elements:
public List<WebElement> getArkiveringsKnapper() {
Wait().until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-e2e-selector='saksjournal-poster']")));
return driver.findElements(By.cssSelector("[data-e2e-selector='saksjournal-checkbox']"));
}
When using the above method (cssSelector for the checkbox element) to find the checkboxes, all three are found. However, Selenium throws the following error
org.openqa.selenium.ElementClickInterceptedException: element click intercepted
Earlier, I've solved this by clicking the label for the checbox instead of the checkbox itself.
But if I use this to find the checkboxes
return driver.findElements(By.cssSelector("[data-e2e-selector='saksjournal-checkbox-lbl']"));
Selenium throws this error:
org.openqa.selenium.ElementNotInteractableException: element not interactable
And if I try using xPath with the first part of the selector (since there are several of them, and they end with -0, -1, -2 etc.):
return driver.findElements(By.xpath("//*[#id=\"^erArkivert-\"]"));
Selenium doesn't find the elements at all.
Any ideas? I'm seeing more and more of these problems in test suites where the code has been working before, but after some change, Angular upgrade or other markup change, it stops working.
I have to answer my own question here, as I was able to find the cause with the help of someone much better with frontend than me.
The problem wasn't with the modal at all, which makes sense since it was able to find the element but just couldn't interact with it.
Also, it's worth remembering that - at least in my humble experience - it's allmost never possible for Selenium (or Cypress, for that matter) to directly click a checkbox. I allmost allways have to click the label element, and then verify the click on the checkbox element.
However, in this case, the label element had a size of zero. So it couldn't be clicked (hence, the "element not interactable" error).
So I solved it by click the element in which the element is placed, by going first to the outside of that, and then clicking the only inside that element.
List<WebElement> elements = driver.findElements(By.cssSelector("[data-e2e-selector=\"saksjournal-checkbox-style\"]"));
for (WebElement element : elements) {
Wait(4).until(ExpectedConditions.visibilityOf(element.findElement(By.tagName("div")))).click();
assertThat(element.findElement(By.cssSelector("[data-e2e-selector='saksjournal-checkbox']")).isSelected()).isTrue();
}

I am unable to click on Button element through my code, could anybody help here

I would like to be able to click on Button through my selenium script using Java.
I tried to do it multiple ways by class, and index in xpath:
1) driver.findElement(By.xpath("contains(#class,'btn btn-alt btn-small tooltip-element') and contains(#tabindex,'0')")).click();
2) driver.findElement(By.xpath("//*button[#tabindex=0]")).click();
My html path is as below:
<a href="#" class="btn btn-alt btn-small tooltip-element" tabindex="0" role="button" data-placement="bottom" aria-label="Help" ng-click="ecdapp.uploadBlueprintModalPopup()" aria-expanded="true" aria-describedby="tooltiptextBtn"> Create <span class="arrow"></span>
</a>
Try this code:
driver.findElement(By.linkText('Create')).click();
It would seem that the link in question is part of a tooltip. Tooltips are only visible when a mouse cursor is hovered over its corresponding web element.
You'll need to hover the cursor over the element in question, then you should be able to locate the link and it should be clickable.

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.

Issue while clicking on 'find hotels' using Selenium-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']

Categories