How to write xpath for below code displayed on Image - xpath

Snapshots displayed Field as well as Inspect element code. Always faced problem on writing xpath for table element. Xpath copied from Moxilla firbugs is worked sometimes but not always.. can any one tell how to write xpath of above code.... Thanks

You can use this xpath
//table[#class='detailList']/tbody/tr/td[contains(text(),'Business Lease')]

Related

How to locate an element having href='#' attribute in anchor tag

abc
I am not able to locate above element. I tried //*[#id="contact-groups"], but with no success.
Well, XPath is not the best of the methods to find elements. But following will match the links with href = "#".
//a[#href="#"]
Not sure if your scenario is as simple as your question. I did test this with a simple html page.
The locator seems correct. You could try this too:
.//*[#id='contact-groups']
As per HTML code provided you can try below xpaths
//a[#id='contact-groups']
//a[contains(text(),'abc')]
//a[#href='#']
Thanks
I am guessing the link is in an Iframe.
Use the following code to switch to the frame and then click on the link
driver.switchTo().frame("frame-name");
driver.findElement(By.xpath("//a[#id='contact-groups']")).click();

Unable to locate the correct element using xpath

I am facing issue in selecting a particular drop down from the webpage.
I need to select the second highlighted div tag in the image above.
The xpath that I am trying to use is:
//div[#class='page-container']//table//div[#class='ui-multiselect-selected-container']
Kindly suggest how can I edit the xpath to select the second div tag.
I am new to xpaths and any help will be appreciated.
I think the below xpath should work to locate the second instance
(//div[#class='ui-multiselect-selected-container'])[2]
The second row has a tr class. When you use //div[#class='page-container']//table//div[#class='ui-multiselect-selected-container'] since your are referring to relative div(using //) it points to 1st element found by default.
I see that the tr element for the second multiselect has a class attribute, which makes unique
so, this will be //div[#class='page-container']//table//tr[#class='rowRelativeTo']//div[#class='ui-multiselect-selected-container']
You can also use the style elements which are unique:
//div[#class='page-container']//table//tr[#class='rowRelativeTo']//div[#style='float:right'] /div[#class='ui-multiselect-selected-container']

XPath Next Page navigation

I'm using Chrome Data Miner, and so far, failing to extract the data from my query: http://www.allinlondon.co.uk/restaurants.php?type=name&rest=gluten+free
How to code the Next Element Xpath for this website? I tried all the possible web sources, nothing worked.
Thanks in advance!
You could look for a tags (//a) whose descendant::text() starts with "Next" and then get the href attribute of that a element.
% xpquery -p HTML '//a[starts-with(descendant::text(), "Next")]/#href' 'http://www.allinlondon.co.uk/restaurants.php?type=name&rest=gluten+free'
href="http://www.allinlondon.co.uk/restaurants.php?type=name&tube=0&rest=glutenfree&region=0&cuisine=0&start=30&ordering=&expand="

How to use Selenium using Xpath to determine the classes of an element?

I am trying to use xpath within selenium to select a div element that is within a td.
What I am really trying to do is determine the class of the div and if it is either classed LOGO1, LOGO2, LOGO3 and so on. Originally I was going to just snag the image:url to determine with logo.jpg was used but whoever made the target website used one image for each logo type and used css to determine which portion of the image will be displayed. So Imagine 4 images on one sprite image. This is the reason why I have to determine the class of the div instead of digging through the css paths.
In selenium I am using storeElementPresent | /html/body/form/center/table/tbody/tr/td[2]/div[3]/div[2]/fieldset/table/tbody/tr[2]/td/div/table/tbody/tr[${i}]/td[8]/div//class | cardLogo .
The div has multiple classes so I am thinking that this is the issue, but any help is appreciated. Below is the target source. This is source from within the table in the tbody. Selenium has no problems identifying all the way up to td[8] but then fails to gather the div. Please help!
<td class="togglehidefields" style="width:80px;">
<div class="cardlogo LOGO1" style="background-image:url(https://www.somesite.com/merchants/images/image.jpg)"></div>
<span id="ContentPlaceHolder1_grdCCChargebackDetail_lblCardNumber_0">7777</span>
</td>
I was fiddling with selenium.getAttribute() but it kept erroring out, any ideas there?
This <div/> element has one class attribute with one value, but this one is tokenized when parsed as HTML.
As selenium only supports XPath 1.0, you will need to check for classes like this:
//div[contains(#class, "LOGO1") or contains(#class, "LOGO2")]
Extend that pattern as needed and embed it in your expression.
With XPath 2.0 and better, you could tokenize and use the = operator which works on a set-based semantics:
//div[tokenize(#class, ' ') = ("LOGO1", "LOGO2")]
Old post but I'll put the solution I used up just in case it can help anyone.
xpath=//div[contains(#class,'carouselNavNext ')]/.[contains(#class, 'disabled')]
Fire of your contains, and then follow with /. to check children AND the current element.

I have xpath written in Selenium RC & now I want to write the same xpath in selenium Webdriver. below is the xpath

I have xpath written in Selenium RC & now I want to write the same xpath in selenium Webdriver. below is the xpath:
"//table[#id='lgnLogin']/tbody/tr/td/table/tbody/tr[4]/td"
by using this xpath, i am capturing the error message displayed on my application like "Please check your Password".
Now how can I write it in Webdriver. I have different ways but, not worked out.
"String msg= driver.findElement(By.xpath("//*[#td='error2']")).toString();" - This is what i did in Webdriver.
Please help me out on this...
The XPath hadn't change from Selenium RC to WebDriver.
If you were able to use the some XPath-expression before it should work in WebDriver too (in 99% of cases as usual).
The code below should work but it's pretty hard to answer without seeing your HTML.
String msg= driver.findElement(By.xpath("//table[#id='lgnLogin']/tbody/tr/td/table/tbody/tr[4]/td")).getText();
//*[#td='error2'] - this expression looks bad as it means "any element with attribute named "td" and value "error2". I suppose that you don't have 'td' attributes in your HTML.
And to the end. I wouldn't recommend to use such long XPath expressions as in your example. It might be broken due to any minor change in layout. It's better to use some specific attributes rather than long hierarchy.
A good way to start would be to start with the basics : http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
What findElement would do is just locate the element that you want to interact with. After you locate the element you need to perform the action that you want to do with the element. In your case, you want the text inside the element. So you can look at appropriate functions of the WebElement interface and use the appropriate method, in your case, getText.

Resources