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");
Related
HTML code:
<div id="routingPanel" class="">
<div id="routingPanelRight">
<ul id="routingList" class="ui-sortable">
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="srl" data-id="15">
<a class="ui-corner-all" tabindex="-1">AS-HTTS-US-LAN-SW</a>
<span class="fa fa-trash"/>
<span class="type">[srl]</span>
</li>
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="queue" data-id="119">
<a class="ui-corner-all" tabindex="-1">AS-EMEA-NORTH</a>
<span class="fa fa-trash"/>
<span class="type">[queue]</span>
</li></ul></div></div>
I need to click on a button which is having the span class"fa fa-trash" but it is inside li class. And i have list on buttons on the page with li class changing.
I am giving testdata from excel file so i can't use the direct value.
i tried to use this xpath
.//*[#id='routingList']/li[5]/span[1] //testdata1
.//*[#id='routingList']/li[2]/span[1] //testdata2
where li value changes everytime from excel file.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//ul[#id='routingList']/li/span[1]")))).click();
List<WebElement> options = driver.findElements(By.xpath("//ul[#id='routingList']/li/span[1]"));
for (WebElement option : options) {
if(testData.equals(option.getText()))
option.click();
Tried above code but it is deleting only one from the list ,where i have passed two more testdata that needs to be deleted.
Need suggestions Please
According to the information you gave me in comments, I think the problem is that you are trying to get a text from an element that doesn't contain text.
Let's say your testData is AS-HTTS-US-LAN-SW. In the HTML you provided and the xpath you mentioned, you are selecting an autoclosing tag <span class="fa fa-trash"/>. Once this tag is selected, you are trying to get the text inside of it, and there is none.
<ul id="routingList" class="ui-sortable">
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="srl" data-id="15">
===========================
<a class="ui-corner-all" tabindex="-1">AS-HTTS-US-LAN-SW</a> ----> The text is contained here
<span class="fa fa-trash"/> ---> No text in that tag
===========================
<span class="type">[srl]</span>
</li>
</ul>
So, basically, you have to modify a little bit your xpath from : //ul[#id='routingList']/li/span[1] to : //ul[#id='routingList']/li/a to get the text, and then go back to the parent node to find your button with : ../span[contains(#class, 'fa fa-trash')]
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//ul[#id='routingList']/li/span[1]")))) // removed the click here because you were clicking on the first element of the list
List<WebElement> options = driver.findElements(By.xpath("//ul[#id='routingList']/li/a"));
for (WebElement option : options) {
if(testData.equals(option.getText()))
option.findElement(By.xpath("../span[contains(#class, 'fa fa-trash')]")).click();
Tell me if it helped
I know you already accepted an answer but there's a more efficient way to do this. You can specify the text you are looking for as part of the XPath. So, you do a single search instead of looping through all the options which can be a performance hit if there are many options. Also, with something like this you are likely to use it more than once so put it in a function.
In this case, the function would take in the string you are looking for and then click the appropriate element.
public void selectRegion(String regionName)
{
driver.findElement(By.xpath("//a[.='" + regionName + "']/following-sibling::span[#class='fa fa-trash']")).click();
}
and you would call it like
selectRegion(testData);
The function looks for an A tag that contains the desired text and then clicks the sibling SPAN with class fa fa-trash.
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*='1168192579894018']
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.
I have the following HTML snippet,
<a onclick="courseOfferingClose(); return false;" href="">
Close
</a>
In this snippet, I'm trying to find the onclick field using firebug firefinder. I have tried the following,
[onclick*='courseOfferingClose']
a[contains(#onclick,'courseOfferingClose')]
[contains(#onclick,'courseOfferingClose')]
But none of these work... Any ideas please
Try css-selector,
a[onclick*='courseOfferingClose']
or Xpath,
//a[contains(#onclick, 'courseOfferingClose')]
<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/
I have a problem to get element <i class="angle-up"></i> on the following page. The selection should be depended on "data-id" value. Thanks in advance.
<li class="recent" data-to="3" data-off="23" data-id="5">
<a class="collapse in" href="/board">
<span>
Board
</span>
<i class="angle-up"></i>
</a>
</li>
<li class="" data-to="3" data-off="23" data-id="7">
<a class="collapse in" href="/set">
<span>
Set
</span>
<i class="angle-up"></i>
</a>
</li>
Well I think if the value is data-id dependant then you might need to split the Xpath or Css selector in 3 parts as below:
Part1=//li[#data_id='
Part2= (Fetch this value at runtime using an appropriate variable)
Part3= ']//i[#class='angle-up']
and now you may use the following code to use the element
public class TestElement {
public static void main(String args[])
{
WebDriver driver= new FirefoxDriver();
driver.get(YOUR INTERNAL URL HERE.);
String elementPart1=//li[#data_id='
String elementPart2=5 //keeping the id you are looking for
String elementPart3=']//i[#class='angle-up']
WebElement element = driver.findElement(By.xpath(elementPart1+elementPart2+elementPart3);
}
}
Here as you may see one example of such xpath could be
//li[#data_id='5']//i[#class='angle-up']
Also if I would advise you to check using firefinder which is a firebug extension to verify that the xpath is correct.