xPath element location - selenium - java

Have following source on the webpage , what I want is to click on the button which is under class fa fa-repeat. Tried to use xpath = "//*[#id=\"accountsList\"//following-sibling::i[#class='fa fa-repeat']" but it doesn't work. Could anyone help/explain me why ? And what could be the best alternative to locate such elements (not necessary via xpath, could be css selector as well) ...
My HTML code is as below:
<li class="list-group-item">
<div class="row account-item">
<div class="col-xs-10">
<span class="label label-danger account-status">O<br>F<br>F</span>
<div><b>assssss</b></div>
<div>company</div>
</div>
<div class="col-xs-2 text-right">
<i data-account="ababa" class="fa fa-repeat"></i>
<i class="account-options fa fa-ellipsis-v" data-toggle="dropdown"></i>
<ul class="dropdown-menu" role="menu">
<li>
<a data-account="fds" class="account-option toggle-active">
<i class="fa fa-play"></i> Start </a>
</li>
</ul>
</div>
</div>
</li>

The target i element is not sibling of *[#id='accountsList'] but descendant :
//*[#id='accountsList']//i[#class='fa fa-repeat']
or using more specific path :
//*[#id='accountsList']/div/div/i[#class='fa fa-repeat']

Related

I Need To Scroll Inside a Drop Down Box

In Selenium, i Was Automating, But in That Drop down Menu i need to Click on The Element Which is Hidden Down.
It scrolls whole page down i want only specific dropdown scrolled down not page.
HTML Code For The Full Div :
<ul class="dropdown-menu drop_Loc" role="menu" style="display: none;" xpath="1"> <li>
<a href="carle_place.aspx" target="_self">
<span class="icon-fort icon-left" style="font-size: 40px;padding: 0px 8px;"></span> <span class="nav_icon_heading">Carle Place</span>
</a>
</li>
<li>
<a href="Selling-a-car-cutler-bay.aspx" target="_self">
<span class="icon2-cutlerbay icon-left" style="font-size: 24px;padding: 7px 8px;"></span>
<span class="nav_icon_heading">Cutler Bay</span>
</a>
</li>
<li>
<a href="Daytona.aspx" target="_self">
<span class="icon-daytona icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Daytona</span>
</a>
</li>
<li>
<a href="Fort_lauderdale.aspx" target="_self">
<span class="icon-fort icon-left" style="font-size: 40px;padding: 0 8px"></span> <span class="nav_icon_heading">Fort Lauderdale</span>
</a>
</li>
<li>
<a href="Sell-my-car-longwood.aspx" target="_self">
<span class="icon-orlando icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Longwood</span>
</a>
</li>
<li>
<a href="Melbourne.aspx" target="_self">
<span class="icon-melbourne icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Melbourne</span>
</a>
</li>
<li>
<a href="miami.aspx" target="_self">
<span class="icon-miami icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Miami</span>
</a>
</li>
<li>
<a href="Orlando.aspx" target="_self">
<span class="icon-orlando icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Orlando</span>
</a>
</li>
<li>
<a href="St_James.aspx" target="_self">
<span class="icon-daytona icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">St James</span>
</a>
</li>
<li>
<a href="Treasure_coast.aspx" target="_self">
<span class="icon-treasure_coast icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Stuart</span>
</a>
</li>
<li>
<a href="Tampa.aspx" target="_self">
<span class="icon-treasure_coast icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">Tampa</span>
</a>
</li>
<li>
<a id="last-child-location" href="West_palm_beach.aspx" target="_self">
<span class="icon-palm_beach icon-left" style="font-size: 40px;padding: 0 8px"></span>
<span class="nav_icon_heading">West Palm Beach</span>
</a>
</li>
</ul>
i Tried With These Code,
By Stuart = By.xpath("/html[1]/body[1]/div[1]/nav[1]/div[1]/div[2]/ul[1]/li[2]/ul[1]/li[10]/a[1]/span[2]");
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement element = driver.findElement(Stuart);
je.executeScript("arguments[0].scrollIntoView(true);",element);
Thread.sleep(4000);
The Above Code is Working,But it is Scrolling The Dropdown aswell as Webpage Also.
Please Sort Me out This Guys.
Your code is right i didn't find any problem in your code it should scroll till that element is visible.
Don't use absolute xpath, it's good practice to always use dynamic xpaths.
Please try below code,
browser.executeScript('window.scrollTo(0,0);').then(function () {
page.saveButton.click();
})
You can also use action class of selenium for scroll,
for that you will need to import it.
import org.openqa.selenium.interactions.Actions;
WebElement menuItem = Driver.findElement(menuItemPath);
Actions action = new Actions(Driver);
action.moveToElement(menuItem);
action.perform();
menuItem.click();

Stripped JSP tags (c:elements)

Good morning all,
I am a Junior Developer and I have been hired on to stream-line the HTML and CSS code for several projects, integrating my HTML and CSS into existing JSPs within Eclipse STS IDE.
Ultimately, I am trying to reduce the code so that it is much cleaner, better organized, and specificity is on the same level. I do this using A-BEM documentation.
Now, when I integrate into the JSPs, I am positive that I am working around any tags and EL statments, careful to maintain any code that would break the project if it were otherwise removed.
Everything works fine when it is tested on my machine.
The issue occurs when I commit my code; my lead developer is finding that I have "deleted all of the JSP tags". And I know for a fact that I have not.
I have a feeling that something is going wrong during the commit process, but I am not entirely sure what the issue could be.
Here is an example of the code that I am given to work with:
<!-- OLD FOOTER -->
<div class="white_bg">
<div class="container">
<div class="top_footer">
<ul>
<li class="left">
<img src="/resources/assets/images/Placeholder text.png"
class="top_footer_logo"/>
<hr class="no_border"/>
Placeholder text.
</li>
<li class="middle">
<br/>
<c:choose>
<c:when test="${isLoggedIn}">
<a href="/booking/BookStepOne"> <i class="fa fa-
calendar-plus-o" aria-hidden="true"></i> Book a
Space </a>
</c:when>
<c:otherwise>
<a href="/Login"> <i class="fa fa-sign-in" aria-
hidden="true"></i> Log In </a>
<br/>
<a href="/Register"> <i class="fa fa-user-plus"
aria-hidden="true"></i> Register </a>
</c:otherwise>
</c:choose>
<br/>
<a href="/ContactUs"> <i class="fa fa-envelope" aria-
hidden="true"></i> Contact Us </a>
<br/>
Hours:<br/> Mon - Fri: 8:30 am - 4:30 pm
</li>
<li class="right">
<br/>
Placeholder text.
<br/>
Placeholder text.
<br/>
San Francisco, CA 94111
<br/>
<a href="placeholdertext"
target="_blank">
<i class="fa fa-map-marker" aria-hidden="true"></i>
Get Directions
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="footer">
<div class="container">
<jsp:useBean id="date" class="java.util.Date" />
©<fmt:formatDate value="${date}" pattern="yyyy" /> Tarkett NA , All Rights
Reserved.
</div>
</div>
And here is an example of my approach:
<!-- NEW FOOTER -->
<section class="o-footer__topSection -whiteBg">
<div class="m-topFooter__left">
<div class="m-logoContain">
<img src="/resources/assets/images/placeholdertext.png" class="a-
freelLogo"/>
</div>
<p class="a-copy">placeholdertext.</p>
</div>
<div class="m-topFooter__mid">
<c:choose>
<c:when test="${isLoggedIn}">
<a href="/booking/BookStepOne"> <i class="fa fa-calendar-plus-o"
aria-hidden="true"></i> Book a Space </a>
</c:when>
<c:otherwise>
<a href="/Login" class="a-botNavLink"> <i class="fa fa-sign-in"
aria-hidden="true"></i> Log In </a>
<a href="/Register" class="a-botNavLink"> <i class="fa fa-user-
plus" aria-hidden="true"></i> Register </a>
</c:otherwise>
</c:choose>
<a href="/ContactUs" class="a-botNavLink"> <i class="fa fa-envelope"
aria-hidden="true"></i> Contact Us </a>
<p class="a-copy">Hours:<br/> Mon - Fri: 8:30 am - 4:30 pm
</div>
<div class="m-topFooter__right">
<p class="a-copy">placeholdertext</p>
<p class="a-copy">placeholdertext</p>
<p class="a-copy">placeholdertext</p>
<a href="placeholdertext" class="a-botNavLink"
target="_blank">
<i class="fa fa-map-marker" aria-hidden="true"></i> Get Directions
</a>
</div>
</section>
<section class="o-footer__botSection -blackBg">
<div class="m-contain">
<jsp:useBean id="date" class="java.util.Date" />
©<fmt:formatDate value="${date}" pattern="yyyy" /> Tarkett NA , All Rights Reserved.
</div>
</section>
Has anyone ever run into this issue? If so, how might this be resolved?
I am using Eclipse STS, Tomcat 8.0 Servers, and SVN version control.
Thanks for taking a look at this!

Skipping over inputting value to drop down menu when not available

Depending on who logs in the page changes from a field having a drop down menu to the field not having a drop down menu and I want my program to ignore that field when the drop down menu is not available. What I have now is this (customCoverageCell targets a cell in Excel)
Select dropDown = new Select(driver.findElement(By.xpath("//*[contains(text(), '"+ customCoverageCell +"')]/following::select")));
if((!customValueCell.equals("") && !driver.findElements(By.xpath("//*[contains(text(), '"+ customCoverageCell +"')]/following::select")).isEmpty())){
dropDown.selectByValue(customValueCell);
}
but it will try to change the drop down menu.
I've tried this
Select dropDown = new Select(driver.findElement(By.xpath("//*[contains(text(), '"+ customCoverageCell +"')]/following::select")));
if((!customValueCell.equals("") && driver.findElements(By.xpath("//*[contains(text(), '"+ customCoverageCell +"')]/following::select")).isEnabled())){//also tried isDisplayed and isSelected just to see if it actually would do anything
dropDown.selectByValue(customValueCell);
}
How would I get it to not try to input a value without making sure the value in excel is blank?
Here is the HTML in question:
WITHOUT DROP DOWN
<div class="wrap-padding-sm">
<div class="threeCols">
<div class="colContent">
<div class="sub-colContent">
<span class="checkmark-grey">
</span>
<span class="checkTitle">Third Party (THIS IS WHERE I NEED TO TARGET AND WORK FROM)
</span>
</div>
<div class="sub-colContent">Limit:
<span class="amount-midCol">$$$$$$$$$$$$
</span>
</div><span class="scroogeMsg h7 mobile-show">Some text</span>
</div>
<div class="colContent">
<a data-toggle="collapse" class="margin-R10" data-target="#cust_standard_scrooge_0">
<span>What this covers
</span>
</a>
<a data-toggle="collapse" data-target="#cust_standard_scrooge_0" class="btn-accordian collapsed"> </a>
</div>
</div>
<div id="cust_standard_scrooge_0" class="scrooge-wrap collapse">
<p>Some text
</p>
</div><span class="scroogeMsg h7 mobile-hide">Some text</span>
</div>
WITH DROP DOWN
<div class="wrap-padding-sm">
<div class="threeCols">
<div class="colContent">
<div class="sub-colContent">
<span class="checkmark-grey">
</span>
<span class="checkTitle">Third Party (THIS IS WHERE I NEED TO TARGET AND WORK FROM)
</span>
<span class="checkTitle_sub">Some text
</span>
</div>
<div class="sub-colContent">
<div class="select-dropWrap">
<div id="h_abc194" class="form-group form-group-lg "><label for="changeScrooge:h_abc161:0:h_abc194:select-one-menu" class="control-label">Limit: </label>
<div class="select-dropMenu ">
<div class="selector uniform-select fixedWidth" id="uniform-changeScrooges:h_abc161:0:h_abc194:select-one-menu"><span style="user-select: none;">1000000</span><select id="changeScrooges:h_abc161:0:h_abc194:select-one-menu" name="changeScrooges:h_abc161:0:h_abc194:select-one-menu" class="form-control input-lg " size="1" onchange="mojarra.ab(this,event,'valueChange','changeScrooges:custom_standard_scrooges','changeScrooges:custom_standard_scrooges changeScrooges:customAcceptDeclinePNL',{'onevent':recommendedScroogeAfterAJAX})">
<option value="1000000" selected="selected">1000000</option>
<option value="2000000">2000000</option>
</select></div>
</div>
</div>
</div>
</div><span class="scroogeMsg h7 mobile-show">Some text</span>
</div>
<div class="colContent">
<a data-toggle="collapse" class="margin-R10" data-target="#cust_standard_scrooge_0">
<span>What text?
</span>
</a>
<a data-toggle="collapse" data-target="#cust_standard_scrooge_0" class="btn-accordian collapsed"> </a>
</div>
</div>
<div id="cust_standard_scrooge_0" class="scrooge-wrap collapse">
<p>Some text
</p>
</div><span class="scroogeMsg h7 mobile-hide">Sme text</span>
</div>
All the black spots are just text fields that had to be blacked out
This is the small trick that i always used to find the existence of an element.
if(driver.findElements(By.xpath("")).size()>0)
{
//Element actually exists
//Do the Stuff
}
else
{
//Element doesn't exist
//Do the stuff if any
}
Hope this helps you. Thanks.

Click the menu that use span class

The code snippet of the page and have the link where I need to click.
<div class="xwq" style="position:absolute;left:0px;right:0px;top:0px;bottom:0px">
<div style="position:absolute;width:100%;height:100%">
<div id="pt1:sdi1" class="af_showDetailItem" style="position:absolute;width:auto;height:auto;top:0px;left:0px;bottom:0px;right:0px">
<div>
<a id="pt1:cb1" class="xfc p_AFTextOnly" href="#" onclick="return false;">
<span class="x106">Cadastro de cliente</span>
</a>
</div>
<div>
<a id="pt1:cb2" class="xfc p_AFTextOnly" href="#" onclick="return false;">
<span class="x106">Relacionar cliente à Proposta de Venda</span>
</a>
</div>
<div>
<a id="pt1:cb3" class="xfc p_AFTextOnly" href="#" onclick="return false;">
<span class="x106">Iniciar processo de Análise de Crédito</span>
</a>
</div>
</div>
</div>
Command in selenium was used :
WebElement menuCadCliente = driver.findElement(By.linkText("Cadastro de cliente"));
menuCadCliente.click();
What has been identified is that a single class and inside it has different values ​​. This is my if I have not understood erroado.
Try this if you are getting an exception when you invoke menuCadCliente.click():
WebElement menuCadCliente = driver.findElement(By.cssSelector("a#pt1\\:cb1 span.x106"));
menuCadCliente.click();

Search for elements with same class names in webdriver

I need to write script for drop-down boxes. There are two dropdown elements on same screen and both the dropdown boxes have got similar class names (Dropdown boxes are of search and select kind),and there is no unique-id for any element.
So the script works fine to select element from first dropdown, But it fails when it supposed to perform same for second dropdown.
Do reply
Regards
<div class="title-edit-form" id="49156080-2097-ea0f">
<div class="control-group required">
<label class="control-label">Title
</label>
<div class="controls row-fluid">
<div class="select2-container span12 select2-container-active select2-dropdown-open" id="s2id_title">
<a href="#" onclick="return false;" class="select2-choice" tabindex="-1">
<span>de</span>
<abbr class="select2-search-choice-close" style="display:none;">
</abbr>
<div>
<b></b>
</div>
</a>
</div>
<input class="span12" id="title" type="hidden" style="display: none;" value="-1">
</div>
</div>
<div class="select2-drop select2-drop-active" style="display: block; top: 177px; left: 536px; width: 530px;">
<div class="select2-search">
<input type="text" autocomplete="off" class="select2-input select2-focused" tabindex="-1" style="">
</div>
<ul class="select2-results">
<li class="select2-results-dept-0 select2-result select2-result-selectable select2-new">
<div class="select2-result-label">
<span class="select2-match">de</span>
</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match">De</span>
"an"
</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted">
<div class="select2-result-label">
"Stu"
<span class="select2-match">de</span>
"nt"
</div>`enter code here`
</li>
</ul>
</div>
the best way is to use cssSelector() for that kind of things, and especially the nth-child() function from cssSelector().
Exemple :
// gets the first div that contains school as a class value.
driver.findElement(By.cssSelector("div.school:nth-child(1)"));
More about cssSelector()
exemple wokring here -> JSfiddle

Categories