Please help in finding the XPath: - java

I've the below snippet, I need to find the Xpath to click on "Option1"
The Problem here is below two divisions are dynamically displayed based on the browser size or Screen Resolution
<div class="wrap">
<aside id="side-anchor-links">
<ul class="menu">
<li><a class="anchor-active">Option1</a></li>
<li"><a class="">Option2</a></li>
</ul>
</aside>
<main class="content">
<div id="wrapper">
<div id="scroller">
<div id="top-anchor-links" class="scroll-me">
<ul class="menu">
<li><a class="">Option1</a></li>
<li><a class="">Option2</a></li>
</ul>
</div>
</div>
</div>
</main>
</div>
Thanks is Advance.
My Trails:
xpath = //aside[#id="side-anchor-links"]/ul/li[text()="Option1"] //This option will work only when the options are displayed in the side view
xpath = //li[text()="Option1"] //This option will retrieve two occurances
Note: Always class="anchor-active" is associated with option1 in Side View until we click on any option from any division

Try with the below xpath :
//div[#class='wrap']/aside[#id="side-anchor-links"]/ul/li[1]

So, I believe you need to click at one element, and if it is not present - on another.
And if plain //li[text()='Option1'] does not suite you well, try this:
//*[#id='side-anchor-links' or #id='top-anchor-links']//li/a
Better customize your //li/a part, however

Related

Selenium-Unable to locate element using xpath for div class

I am using selenium webdriver in java.I neead to click a menubar. i tried various xpaths and not working. <div class="menu-toggler sidebar-toggler"> </div> is the element i am trying to click. I used the xpath /html/body/div/div[4]/div[2]/div/div[2]/div[2]/div[2]/div/div/div[1]/div[2]/a.
I am fine with any option that would help me click the menu bar.Stuck here with automation.i use ngwebdriver framework so if it can be done using ngwebdriver is also fine.It would be really great if somebody could help me with this.
<div class="ng-scope" ng-if="loggedIn">
<div class="page-spinner-bar hide" ng-spinner-bar="">
<div class="ng-scope" data-ng-controller="HeaderController">
<div class="page-header md-shadow-z-1-i navbar navbar-fixed-top ng-scope" data-ng-include="'app/main/tpl/header.html'">
<div class="page-header navbar navbar-fixed-top ng-scope">
<div class="page-header-inner">
<div class="page-logo">
<div class="menu-toggler sidebar-toggler"> </div>
</div>
<a class="menu-toggler responsive-toggler" data-target=".navbar-collapse" data-toggle="collapse" href="javascript:;"> </a>
<img class="small-logo" src="assets/img/logo_kart_small.gif">
<div class="top-menu">
</div>
</div>
</div>
</div>
You should try using By.cssSelector() as below:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement sideMenuButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.menu-toggler.sidebar-toggler")));
Actions actions = new Actions(driver);
actions.moveToElement(sideMenuButton).click().perform();
Edited :- If unfortunately above does not work try using JavascriptExecutor to perform click as below :-
WebElement sideMenuButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.menu-toggler.sidebar-toggler")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", sideMenuButton);
Try using firefox's firebug add on that has a built in xpath generator. It's a really accurate generator. Has solved many problems of mine even if I was using a different browser than firefox.

How to click Buy Now button using selenium?

Source HTML look like this :
<script id="during-reserve-tpl" type="text/x-lodash-template">
<div class="gd-row">
<div class="gd-col gu16">
<div class="emailModule message module-tmargin">
<div class="error-msg"></div>
<div class="register brdr-btm">
<div class="jbv jbv-orange jbv-buy-big jbv-reserve">Buy Now</div>
</div>
<div class="topTextWrap brdr-btm tmargin20">
<div class="subHeading">
Only one phone per registered user
<p>
First come, first serve!
</p>
</div>
</div>
</div>
</div>
</div>
</script>
When I code : IWebElement buy = driver.FindElement(By.CssSelector(".jbv.jbv-orange.jbv-buy-big.jbv-reserve")); It says Element not found.
I tried putting By.ClassName with while spaces but it says, compound classes are not supported.
Is there any alternative to click it ?
driver.FindElement(By.cssselector("div.jbv.jbv-orange.jbv-buy-big.jbv-reserve"))
In the above example css selector looks for div tag with name and it will look for all the dot with space
Try this By.xpath("//*[contains(#class, 'jbv')]") if it works.
You can try either of these:
IWebElement buy = driver.FindElement(By.CssSelector("div.register>div"));
OR
IWebElement buy = driver.FindElement(By.CssSelector("div.register"));

How to retrieve atomic values inside tags in HTMLUnit

I am new to HtmlUnit and I don't know how to get the text inside the [...]
A part of my html file:
<ul ......somethin....>
<li data-role="list-divider" role="heading" style="font-size:16px;" class="ui-bar-f">
INFORMATION_LINE_1
</li>
<li data-theme="d" class="ui-li ui-btn-icon-right ui-btn-up-d ui-odd-match-column ">
<div class="ui-btn-inner ui-li">
<div class="">
<div class="ui-btn-text">
<a href="/x/cxntay/13113/ndzvsssl/g1" class=" ui-link-inherit ui-link-hover">
<h3 class="ui-li-heading">
<span class="xheader">INFORMATION_LINE_2</span>
<span class="label live">INFORMATION_LINE_3</span>
</h3>
<div class="ui-live-scores">
<span class="team1-scores">
<span class="ui-team-name">INFORMATION_LINE_4</span>
<span style="font-weight:bold">INFORMATION_LINE_5</span>
</span>
<span>INFORMATION_LINE_6</span>
</div>
</a>
</div>
</div>
</div>
</li>
</ul>
Now, I want to retrieve "INFORMATION_LINE_X"(1,2...6) in between these tags..
This is what I tried:
List<HtmlUnorderedList> ls = (List<HtmlUnorderedList>) page.getByXPath("/ul");
List<DomNode> dls = ls.get(0).getChildNodes();
System.out.println(dls.get(0).getFirstByXPath("//li[#data-role='list-divider']/text()");
I just tried to get INFORMATION_LINE_1
But it printed null. I need to get all the INFORMATION_LINES.
It is better to use just XPath rather than mixing it with HTMLUnit methods. Something like this should work to get you the first information line:
HtmlElement e = page.getFirstByXPath("//li[#data-role='list-divider']");
System.out.println(e.asText());
In order to fetch the other information lines you should follow the same approach but changing the XPath string.
Bear in mind you should always debug the page by taking a look at the code by printing the output of page.asXml(). If you use a real browser you are not actually seeing exactly the same as HTMLUnit is seeing. You can stumble with differences particularly if the page executes JavaScript.

Unable to find element with Xpath

I'm trying to find out the xpath for resource manager element, when I tried with this code,
driver.findElement(By.xpath("Resource Manager xpath")).click(), I turned up with error saying unable to find the element.
<div class="os-titlebar navbar navbar-inverse">
<div class="navbar-inner">
<div class="container">
<ul class="nav menu-primary os-navbar-icon">
<ul class="nav context-menu" style="">
<ul class="nav os-navbar-icon os-titlebar-shortcuts pull-right os_shortcuts">
<li class="os-navbar-icon-home selected">
<li class="os-navbar-icon-quicklaunch os_quick_start">
<li class="os-navbar-icon-resourcemanager">
<li class="os-navbar-icon-apptray">
<li class="os-navbar-icon-notifications">
<li class="os-navbar-icon-minimise-close">
</ul>
<form class="navbar-search pull-right ui-os-search-form" action="">
<ul class="nav os-navbar-icon os-desktop-navigation" style="left: 407.5px;">
</div>
Resource Manager xpath is not a valid xpath expression.
This should work:
driver.findElement(By.xpath("//li[#class='os-navbar-icon-resourcemanager']")).click()
Use css selector instead of xpath. Also an example of WebDriverWait
WebDriverWait wait = new WebDriverWait(driver,300/*timeout in seconds*/);
By findBy = By.cssSelector("li.os-navbar-icon-resourcemanager");
wait.until(ExpectedConditions.elementToBeClickable(findBy)).click();
you may face "Unable to find element" only in two case for sure.
1.The Element is yet to Load
- Put some wait here.
2. You may try to locate the element wrongly .
- Double Check your Xpath/ID(what ever)
- Make sure you are in the same frame where the element is present.If not, switch to the frame then.

How to click a link from list selenium2

I'm using Selenium 2 and I want to click an 'invite' link for Name3. How can I do that?
here is the html code:
<ul>
<li>
<label for="511565484">
<img src="pic1">Name1</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
<li>
<label for="535963597">
<img src="pic2">Name2</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
<li>
<label for="561708219">
<img src="pic3">Name3</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
</ul>
Seems likely it can only be done with XPath:
//label[text()='Name3']/following-sibling::a
element2 = driver.findElement(By.xpath("//img[()text='Name3']/a"));
element2.click();
If XPATH isn't the most usable thing for you, you can always do something like this (Ruby implementation of Webdriver... but it's all the same):
invite_links = driver.find_elements(:class_name, "sendInvite")
invite_links now contains an array of all matches, so your next step is pretty easy:
invite_links[2].click()
Or the way I'd do it:
driver.find_elements(:class_name, "sendInvite")[2].click
This is a little easier for me to read than XPATH, because I don't use it that often.

Categories