XPATH identifier from HTML - selenium

<ul class="popupmenu is-open" id="menuFormDefaultFC" role="menu" aria-hidden="false">
<li role="presentation"><a tabindex="-1" role="menuitem" href="#A">Add</a>
Please somebody let me know the Xpath for above html with href in it.

Here are a few options for href.
1. By.xpath(".//a[#href='#A']");
2. By.xpath(".//li[#role='presentation']/a[#href='#A']");
But for links you could use just linkText. I prefer below for simplicity unless your app supports multiple locales.
By.linkText("Add");
Or use CSS selectors
1. By.cssSelector("a[role='presentation']");
2. By.cssSelector("li[role='presentation']>a[href='#A']");

Try this //a[contains(text(),'Add')]
It may be used as driver.find_element_by_xpath("//a[contains(text(),'Add')]")

I would do it this way:
By closedMenu = By.xpath(".//ul[not(contains(#class, 'is-open'))]");
By openMenu = By.xpath(".//ul[contains(#class, 'is-open')]");
By addItem = By.linkText("Add");
By addItemLocator = new ByChained(openMenu, addItem);

Use Firefox plugin "Firepath" to extract xpath.
https://addons.mozilla.org/en-US/firefox/addon/firepath/

Related

Not able to identify custom checkbox element/Need relative xpath

I need relative xpath of the span element
<span class="icon"></span>
in selenium web driver.
<span class="custom-checkbox">
<input id="personalization1" name="personalization-terms-check" class="personalization-terms-check required" value="accept" type="checkbox">
<label for="personalization1">
<span class="icon"></span>
<span class="order-ready-text">yes! I double-checked all personalization entered above and i'm ready to order</span>
</label>
</span>
need relative xpath of the span element
<span class="icon">
in above html and I am getting relative path:
//*[#id="main"]/div[3]/div[4]/div[2]/div/span/label/span[1].
Please help me to get an relative xpath or another way to make an click on the span
NOTE:
<span class="icon">
It can be multiple so I need unique relative xpath.
You can wait for the element visibility before performing any action like below.
WebElement element=driver.findElement(By.xpath("//*[#name='personalization-terms-check']/following-sibling::label/span"));
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element));
element.click();
and you can use different xpath or id to locate the element. the above xpath map be unique if only one personalization term checkbox on the page.
Can you try with this xpath?
"//label[#for='personalization1']/span[1]"
Hope this helps. Thanks.
Try this:
Try this XPath -
//input[#id='personalization1']/label/span
Try this xpath to click on checkbox:
.//input[#id='personalization1']
or you can use this xpath
.//span[contains(text(),'yes! I dou')]

Get xpath for div inside td tag

I tired a lot to get the xpath of the div which displays as "カーク商品掲載カタログ Online" on screen, but I'm not able to. The application is made using ZK framework hence it has random ID's.
<td z.type="Lic" id="z_mk_qr1" class="activeCatalogVersionTreeItem z-list-cell" z.zcls="z-list-cell" onmouseout="dragHoverClick( $e('z_mk_qr1'), event, true, null, 0);" onmouseover="dragHoverClick( $e('z_mk_qr1'), event, false, 'PerspectiveDND', 500);">
<div id="z_mk_qr1!cave" class="z-list-cell-cnt z-overflow-hidden">カーク商品掲載カタログ Online
<span id="z_mk_rr1" class="catalog-mnemonic-label z-label" z.zcls="z-label"> (STO-O)
</span>
</div>
</td>
I thought the xpath should be something like //div[contains(text(),'カーク商品掲載カタログ Online')], but it doesn't work.
Thanks!
Try the below cssSelector to identify the required element.
td.activeCatalogVersionTreeItem div.z-list-cell-cnt
You may use partial text match in this case as follows:
//div[contains(text(), 'Online')]
Check the link below for more:
https://sqa.stackexchange.com/questions/10342/how-to-find-element-using-contains-in-xpath

How to Click On Group Add Member through facebook

I want to click on group add button of Facebook, but I have not found it possible using an xpath.
Here is the html for the button:
<li>
<a
class="_42ft _4jy0 _3-8i _4jy3 _517h _51sy"
role="button"
href="#"
ajaxify="/ajax/groups/members/add_get.php?group_id=1168192579894018&refresh=1"
rel="dialog">
<i class="_3-8_ img sp__lkuGKPb9f- sx_e8790e"></i>
Add
</a>
</li>
This is how I have tried to click on it:
JavascriptExecutor jse1 = (JavascriptExecutor)driver;
jse1.executeScript("document.getElementById('gbqfb').click‌​();");
You should try using xpath as below :-
driver.findElement(By.xpath(".//a[contains(.,'Add')]")).click();
Or
driver.findElement(By.xpath(".//a[normalize-space() = 'Add']")).click();
I don't see the id that you are using.
You can try with the following css selector:
a[href*='groups/members/add'][href*='116819257‌​9894018']
The first href assure that you are clicking the add for the group,
the second href assures that you are clicking add for the right group
I don't know how the page is looking, if you have only one add then you can remove the second href.
If if you can get the selector with css then for sure you can get it with xpath.
I recommend using css.

how to find the xpath for the below code

Trying to find the xpath for the below face book link .
<a class="home_fb_logo" target="_blank" title="facebook" href="http://www.facebook.com/chaturdn"></a>
<a class="home_tw_logo" target="_blank" title="twitter" href="http://www.twitter.com/chaturdn"></a>
<a class="home_sc_logo" target="_blank" title="soundcloud" href="http://www.soundcloud.com/chaturdn"></a>
<a class="home_go_logo" target="_blank" title="google+" href="https://www.google.com/+dnchaturvedi/about"></a>
<a class="home_yt_logo" target="_blank" title="youtube" href="http://www.youtube.com/chaturdn"></a>
<a class="home_li_logo" target="_blank" title="linkedin" href="http://www.linkedin.com/in/chaturdn"></a>
</div>
You can use this:
//a[#class='home_fb_logo']
use this //*[#title="facebook"]
to get the link find the element //*[#title="facebook"] and use getAttribute("href") to get the link in selenium.
To get a handle on the Facebook link WebElement you could use either of the following XPath expressions:
//a[#class='home_fb_logo']
//a[#title='facebook']
To get the actual URL from the link WebElement you can use the WebElement.getAttribute() method like so:
String url = element.getAttribute("href");

Finding all "A" tags with specific strings in the attribute href?

driver.FindElement(By.Name("zipcode")).Clear();
driver.FindElement(By.Name("zipcode")).SendKeys(zipcode);
driver.FindElement(By.Name("Go")).Click();
driver.FindElements(By.TagName("A"). //<---- ?????????
I have some Selenium API code that I started. I aim to get all the "A" tags with the string "alertsepy" and the sting "sevendwarves" in the attribute href and return all those elements into an array so I can do some further processing. I started the code but I am really not quite sure how to get all the way there yet. Does anyone know how to do this type of query with Selenium.
Kind Regards!
You should use css selector:
IList<IWebElement> elements = driver.findElements(By.cssSelector("a[href*=alertsepy],a[href*=sevendwarves]")
This query will return a nodes with href attribute that contains alertsepy or sevendwarves or both strings:
<a href="alertsepy.html" > </a>
<a href="sevendwarves.html" > </a>
<a href="http://sevendwarves.org/alertsepy.html" > </a>
Or you can use:
IList<IWebElement> elements = driver.findElements(By.cssSelector("a[href*=alertsepy][href*=sevendwarves]")
This query will return a nodes with href attribute that contains alertsepy and sevendwarves strings:
<a href="http://sevendwarves.org/alertsepy.html" > </a>
For a list of generally available css selectors refer to w3c css selectors. For the list of available in Selenium query types refer to Locating UI Elements.
List<WebElement> anchortaglist = driver.find Elements(By.Tag Name('a');