Understanding XPath
Following my latest article "CssSelectors not only for styling" many people asked me about the difference between using CssSelector and XPath, and what are the benefits to using XPath as a locator. Sadly, I see many engineers struggling to identify an element by using other selectors, find themselves blindly copying a WebElement's XPath using the option provided by the browser. The deal is that the expression received is mostly highly flaky and ugly none the less.
In this article, I'm going to cover this topic and see if we can demystify XPath and make it more accessible.
What is XPath?
XPath stands for XML Path Language. It is basically a Quarry language that was originally made for selecting nodes from an XML document that was defined by the W3C. XPath can be used for XML schemes and also in many programming languages (Such as Java, Python, PHP, and even C).How is it related to Test Automation?
XPath is one of the 8 locating methods (9 if you count relative locators added in Selenium 4 alpha). to identify elements in the application under test.By using XPath we can query an element in a more flexible way, locating it by the full or relative path in the document object model and even use some of its internal libraries to compute a specific element by defining several conditions within the expression. XPath is a highly flexible and strong locating method. It allows us to locate elements by a static visible text, elements with no unique properties or even without any attributes at all.
How to use XPath to locate WebElements?
For this part, I'm going to use Chrome Dev Tools to demonstrate some usage examples.The website I'm using is Google.com's main search page.
Understanding the syntax
Full path to a WebElement:
Let's say I want to query the first div on this webpage using its full path in the DOM which is:
/html/body/div
What does this expression mean?
Relative path
In most cases using a relative path would be preferred. Why?Let's look at a more complex example. Let's say that I want to identify the search text field.
/html/body/div/div[3]/form/div[2]/div[1]/div[1]/div/div[2]/input
In the <html> tag look for the <body> tag, find the first <div>, in which look for the third <div>, find the <form> tag in it, look for the third <div> in that tag... well, you get the point.
If even one of the "Stops" leading to my target in this expression "Breaks", the expression will no longer lead to the desired element.
Basically, I can identify the same element by using a shorter expression such as:
//input[4]
or a stronger one:
//input[@type="text"]
Locating by static visible text
Let's say that we want to locate this text:

Well, It has an ID but if it didn't I could choose to locate it via its static text with the following XPath syntax:
//div[text()='Google זמינה ב: ']
But, what if only a part of the text is static?
Well, in that case, I could choose to find it with the "contains" keyword.//div[contains(text(),'Google')]
or:
//div[starts-with(text(),'Google')]
What about an element's parent?
I can use the same expression to locate that element's parent. Fo example://div[@id='SIvCob']/parent::div
What if I want to find all elements on a page containing a particular text?
I can use Wildcards to find this element without specifying a particular tag//*[contains(text(),'Google')]
Locating elements by providing multiple attributes
If needed, we can use multiple attribute selection in our XPath syntax. For example, I can locate the Search button by both name and type:
//input[@class='gNO89b'][@type='submit']
//input[@class='gNO89b'][@type='submit']
In conclusion:
Both CSS and XPath are highly powerful locating strategies. Depending on the use case you can benefit each of them as they provide you with great flexibility. XPath can navigate up the DOM while CSS cannot (As far as I know) and some say that at least in IE CSS is much faster.
By the way, there are websites such as cssify that help you convert your XPath expression to a CSS selector.
By the way, there are websites such as cssify that help you convert your XPath expression to a CSS selector.
Great Information you have shared, Check it once selenium online training
ReplyDelete
ReplyDeleteIt's very useful blog post with inforamtive and insightful content and i had good experience with this information.
Selenium Training in chennai | Selenium Training in annanagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery
adanaescort01.com - adiyamanescortxx.com - afyonarackiralama.net - aksarayescort.net - antalyaoyunpark.com - aydinescortkiz.com - balikesirescortlar.com - batmanescortlar.com - bitlisescortlar.com - burdurescortlar.com - bursamalaysias.com - diyarbakirambar.com - edirnedespor.com - erzurumyolkosusu.com - eskisehirescortlari.com - gaziantepekspres.org - gebzeescortkiz.com - giresunmaraton.com - hataykoleji.com - ispartakpss.com - karabukteknik.com - kastamonuajans.net - kayserivalisi.com - kilisescort.com - kocaeliescortlar.com - konyaescortlar.com - kutahyaizemlak.com - malatyadataksi.com - manisaescortlar.com - marasatasoyemlak.com - mardinfanatik.com - mersinmoda.com - muglaapart.net - nigdeyapi.com - orduescortt.com - osmaniyeyorum.com - sakaryanur.com - samsunescortlar.com - siirteyatirim.com - sincanoto.com - tekirdagescortlar.com - tokatforum.com - usakbasin.com - vanescortilan.com - yalovadaemlak.com - yozgattanal.com - sanliurfadayim.com - zonguldakescort.com
ReplyDeletecami avizesi - no deposit bonus forex 2021 - takipçi satın al - takipçi satın al - takipçi satın al - takipcialdim.com/tiktok-takipci-satin-al/ - instagram beğeni satın al - instagram beğeni satın al - btcturk - tiktok izlenme satın al - sms onay - youtube izlenme satın al - no deposit bonus forex 2021 - tiktok jeton hilesi - tiktok beğeni satın al - binance - takipçi satın al - uc satın al - sms onay - sms onay - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - takipcialdim.com/instagram-begeni-satin-al/ - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - perde modelleri - instagram takipçi satın al - takipçi satın al - instagram takipçi satın al - betboo
ReplyDeletehttps://www.escortsmate.com - adana escort - adıyaman escort - afyon escort - ağrı escort - aksaray escort - amasya escort - ankara escort - antalya escort - ardahan escort - artvin escort - aydın escort - balıkesir escort - bartın escort - batman escort - bayburt escort - bilecik escort - bingöl escort - bitlis escort - bolu escort - burdur escort - bursa escort - çanakkale escort - çankırı escort - çorum escort - denizli escort - diyarbakır escort - düzce escort - edirne escort - elazığ escort - erzincan escort - erzurum escort - eskişehir escort - gaziantep escort - gebze escort - giresun escort - gümüşhane escort - hakkari escort - hatay escort - ığdır escort - ısparta escort - izmir escort - istanbul escort - karabük escort - karaman escort - kars escort
ReplyDeleteI read your blog now share great information here. Vonex phones
ReplyDeleteseo fiyatları
ReplyDeletesaç ekimi
dedektör
instagram takipçi satın al
ankara evden eve nakliyat
fantezi iç giyim
sosyal medya yönetimi
mobil ödeme bozdurma
kripto para nasıl alınır
instagram beğeni satın al
ReplyDeleteyurtdışı kargo
seo fiyatları
saç ekimi
dedektör
fantazi iç giyim
sosyal medya yönetimi
farmasi üyelik
mobil ödeme bozdurma
smm panel
ReplyDeletesmm panel
is ilanlari blog
İNSTAGRAM TAKİPÇİ SATIN AL
hirdavatciburada.com
beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi
Good content. You write beautiful things.
ReplyDeletevbet
mrbahis
taksi
vbet
hacklink
korsan taksi
mrbahis
sportsbet
hacklink
Good text Write good content success. Thank you
ReplyDeletemobil ödeme bahis
betpark
kibris bahis siteleri
slot siteleri
poker siteleri
bonus veren siteler
tipobet
kralbet