css locator to navigate to parent tag from child and css locator to identify sub string in a attribute - html

I'm good at writing XPATH locators but for my new project i have to write locators using CSS
i have 2 doubts
1)
from the above code i have to identify div with id contains value "col3"
in xpath i can write //div[contains(#id,'col3')]
in css i tried below locators
css=#id:contains('col3')
css=div:contains(#id,'col3')
but none of them working.
please help me with CSS locator to identify id contains value "col3"
2)
i have to navigate from one attribute to its parent attribute.
i.e., from "Form tag i have to navigate ti its parent div tag"
in XPATH //form[#id='comment-form']/../
help me to navigate using CSS

Can you try this for 1st one
css=div[id*='col3']

The solution to your first problem is:
div[id*='col3']
Referring to Is there a CSS parent selector?, I do not think there is any solution for your 2nd problem. You might have to use xpath here.

Related

XPath for element by attribute value in Selenium?

For the following HTML:
Why does the following XPath doesn't work:
//option[value='0']
Is it related to value or to option element?
//option[value='0']
is not a valid selector incase you are attempting to identify/select the respective <option> element using Selenium.
Solution
You can use either of the Locator Strategies:
xpath:
//option[#value='0']
css_selector:
option[value='0']
tl; dr
Why should I ever use CSS selectors as opposed to XPath for automated testing?
Change
//option[value='0']
to
//option[#value='0']
because value is an attribute, not an element.
Your current xpath is searching for an option element with a child element value which in turn has contents 0. What you actually want to find is an option element with an attribute value with value 0:
//option[#value='0']

How to access <a> tag which is present inside <th> in VBA that doesn't have id or classname?

I'm trying to automate the web form where I have <a> tag which is inside <th> tag. When I tried getElementByTagName("a").innerText I'm not getting the desired element/text. But when I wrote getElementByTagName("th").innerText it is showing me the exact text that I'm pointing at. But the issue is I wanted to click on the link which this text i.e <a> tag has. getElementByTagName("th").Click is not working. Can someone please help?
There's no such method as getElementByTagName().
There are: document.getElementsByTagName() and Element.getElementsByTagName(). Both return a live HTMLCollection.
In the latter Element refers to a DOM element. It allows you to search for specific tags in children of that element.
Plese refer to the following MDN documents:
Element.getElementsByTagName()
Document.getElementsByTagName()
Also, it's worth mentioning that, without document.* or anything else, the browser would assume you're trying to call window.getElementByTagName().
NOTE: I'm aware the question is tagged vba instead of javascript, but in this case it doesn't seem to matter.

Get Xpath for href from a tag based on span tag value

I am a beginner to xpath and I am unable to get XPath to get link from 'a' tag for below HTML code.
Get HREF value where span class value is "Upholstered" as shown in the snapshot.
Here, I want this value "/furniture/Bedrooms/Queen-Beds/_/N-8ddZ1z141u9?qf=styles_Upholstered" using Xpath.
Can you help me out please
According to your description the relevant XPath query to Get href value where span class value is "Upholstered will be something like:
//a[#class='Upholstered']/#href
However you forgot to add your actual HTML code (at least partial) so the above answer might not be 100% accurate.
Reference material:
XPath Language Specification
XPath Tutorial
Using the XPath Extractor in JMeter
Use below xpath to extract the URL of your <a> tag
//ul[#class='facetOptions']/li/a[#role='checkbox']/#href

Convert to CSS Selector

Trying to convert the below given HTML tag of a Image Button which I want to click but not getting clicked while using Xpath.
HTML Script
<img src="../../../../imagepool/transparent%21tmlservicedesk?cid=1"
id="reg_img_304316340" aralttxt="1" artxt="Show Application List"
arimgcenter="1" alt="Show Application List" title="Show Application List"
class="btnimg" style="top:0px; left:0px; width:23px; height:140px;">
Xpath Generated for the same:
//div[#class='btnimgdiv']/img[#id='reg_img_304316340']/#src
Read some of the articles that for image buttons CSS selector is much better than xpath and wanted to know how to convert the html to CSS selector.
Image BUtton which i want to click but not getting clicked while using Xpath
This is because you are using id attribute value of the element which looks like dynamically generated.
Read some of the articles that for image buttons CSS selector is much better than xpath
Yes, you are right, using cssSeector is much faster than xpath to locate an element.
wanted to know how to convert the html to CSS selector.
You need to use that attribute value which is unique and unchangeable to locate element, you can use below cssSelector :-
img.btnimg[title='Show Application List']
Reference Link :-
http://www.w3schools.com/cssref/css_selectors.asp
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

Xpath and innerHTML

What Xpath expression can I use to find all the anchor (just 'a') elements whose actual text (the innerHTML) is Logout.
something like
//a[#innerHTML='Logout']
Would that be correct?
No, it would be incorrect. innerHTML is a property, part of the object model, while XPath operates on tags and attributes. Unless your a tag actually has an attribute named innerHTML, this wouldn't work.
If you want to compare the value of the tag itself, you can use the . (dot) to refer to the tag:
a[.='Logout']
However, I must add, just in case you're using jQuery: I'm not sure if it will work with jQuery. jQuery does not support XPath fully, only basic stuff.