Unable to locate the correct element using xpath - 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']

Related

How to locate a link with an img on the same row in a grid with xpath

I need to click a link in a grid that contains a particular img on the same row. I can easily find the correct img I want with:
//img[#src='./assets/images/not_evaluated.svg'].
However, when I issue the click with it, it doesn't work because the image is not clickable. However, the same row contains the link I want to click. The xpath to the clickable link is:
//a[#class='veracodelink']//span[contains(text(),'Static Scan')].
I need to combine these 2 xpaths to get the correct link to click. I have tried many different things and combinations, but nothing seems to work.
Assuming that the image and the link are inside the same tr element, you could combine them like this:
//tr[.//img[#src='./assets/images/not_evaluated.svg']]//a[#class='veracodelink']//span[contains(text(),'Static Scan')]
Explained: //tr[...] here selects the correct row containing the image, and the rest then selects the clickable element relative to that.
If they are inside something else than tr, adjust accordingly.
So I finally figured this out myself: The xpath I needed is:
//div//vc-link//a[#class='veracodelink']//span[contains(text(), 'Static Scan')][../../../../../..//img[#src='./assets/images/not_evaluated.svg']]

How xpath works for tags in tags

I am trying to find out the xpath for first name of the facebook page and I have ended it with the following xpath: "**//div[1]/div[1]/div[1]/div[1]/input[#class='inputtext _58mg _5dba _2ph-']**" which is correct. My question is that, there are total 9 div tags on the page but I got it with the fourth div, I am not getting the reason how it's finding it in fourth div?
Page is Facebook home Page and element to find with xpath is Fist name input box
Please help me to understand how it's finding the element using above xpath
I know there are other ways to find xpath but I want to know the reason how it's finding it
I hope I am providing the complete information for the asked question if not let me know
Well it's because your xpath starts with a //. In literal english, it says find a DIV whose child is a DIV whose child is a DIV whose child is a DIV whose child is your INPUT. In your case, it does find a DIV which has INPUT as described by your xpath.
If you replace that // to single /, it will find the first DIV and then will try finding your input. Which it won't be able to find since .. like you said there are 9 DIVs.
Hope that paints a picture. Let me know if you need more explanation.

Watir how to click on nested element

I'm trying to click on "Mr" from the drop down list I've tried a combination of things but non of them seem to work.
I've even tried xpath which is usually reliable but for this case its failing.
$browser.element(:xpath, "/html/body/div[1]/div[1]/div[1]/div/div[2]/div[1]/div[2]/div/div[2]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div/div/div/div/ul/li[2]/a").click
The XPath suggested by Saurabh Gaur, can be written in a more readable Watir-like fashion using:
$browser.ul(class: 'dropdown-menu').link(text: 'Mr').click
Note that this assumes that there is only one ul element with class dropdown-menu. If there are multiple, you will need to scope the search to the specific dropdown using an element that likely exists higher in the DOM.
However, given there is likely only one link with text "Mr", you can probably get away with simply:
$browser.link(text: 'Mr').click
Given the link is a dialog that switches from hidden to visible, you may need to also wait:
$browser.link(text: 'Mr').when_present.click
Your xPath is positional which depends on element position.. it will not work if elements are change their position means adding some elements after some action on the page.
After seeing your attached image I have generated following xPath as below :-
//ul[contains(#class, 'dropdown-menu')]/descendant::span[contains(.,'Mr')]/parent::a
Try with this xPath.. May be it will work...:)

Retrieve value from within a div tag in xpath

I am trying to retrieve the value in the data-appid field, I have tried using following-sibling but its not really a sibling per se. Not sure how to go about retrieving this. Any pointers will really be great.
<div class="section app" data-appid="532054761" data-updateid="10184169">
The sibling axis applies to elements, not attributes.
You can reference data-appid simply as an attribute of the div element. For example,
//div/#data-appid
will select 532054761
If you need to be more specific about the particular div element for which you want its data-appid, you can use a predicate to select a particular div element. For example:
//div[#data-updateid='10184169']/#data-appid
You need to use getAttribute() method
Try the following code
String dataAppId = driver.findElement(By.xpath("//div[#class='section app']")).getAttribute("data-appid");
System.out.println(dataAppId);

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.

Resources