Selenium CSS Selectors Examples

Ukufumana izinto ngabakhethi beCSS yeyona ndlela ikhethwayo njengoko ikhawuleza kwaye ifundeka ngakumbi kune-XPath.

Olu qeqesho lubonelela ngemizekelo yendlela yokufumana izinto zewebhu kwiSelenium usebenzisa abakhethi beCSS.



Abakhethi beCSS ngoNcedo

Masicinge ukuba sinethegi enezi zinto zilandelayo [id, iklasi, igama, ixabiso]


Indlela eqhelekileyo yokufumana izinto ngophawu yile:

css = element_name[='']

Umzekelo:


WebElement firstName = driver.findElement(By.cssSelector('input[name='first_name']'));

Uphawu lwe-Id

Kwi-CSS, sinokusebenzisa # Isaziso sokukhetha id uphawu lwento:



Umzekelo:

driver.findElement(By.cssSelector('input#firstname')); //or driver.findElement(By.cssSelector('#firstname'));

Inqaku leklasi

Umgaqo ofanayo unokusetyenziselwa ukukhangela izinto ngokwazo class uphawu.

Sisebenzisa . ubhalo.


driver.findElement(By.cssSelector('input.myForm')); //or driver.findElement(By.cssSelector('.myForm')); Phawula:Thatha inkathalo engaphezulu xa usebenzisa i . Isaziso njengoko kunokubakho izinto ezininzi zewebhu kumthombo we-HTML onempawu ezifanayo zeklasi.

Iimpawu ezininzi

Ngamanye amaxesha kukho imfuneko yokuba ngakumbi ngakumbi kunye neendlela zokukhetha ukuze ufumane into echanekileyo.

Ixabiso lomboniso linokuthi 'akukho' okanye 'ibhloko' kuxhomekeke kwifowuni yeAjax. Kule meko, kufuneka sibeke into ngokodidi kunye nesitayile.

Umzekelo:

driver.findElement(By.cssSelector('div[class='ajax_enabled'] [style='display:block']'));

Nika HAYI iqulethe ixabiso elithile

KwiWebDriver, uzifumana njani izinto ezinophawu lwamaxabiso ongafuniyo ukulikhetha? Lo mzekelo wokukhetha weCSS ubonakalisa ukuba HAYI ukukhetha ngexabiso elithile lophawu


Masithi unezinto ezininzi ezabelana ngeempawu ezifanayo kunye nexabiso lophawu, kodwa ezinye zezinto zinokunye okwahlukileyo okunconyelwe kwixabiso. Umzekelo:

Kule snippet ingentla, sifuna ukukhetha usuku olukhoyo (okt ezimbini zokugqibela div element)

Njengoko kunokubonwa, zonke ii-divs ezine ziqukethe 'usuku lwekhalenda-' kodwa ezimbini zokuqala zine 'azifumaneki' esingazifuniyo.

Umkhethi weCSS wokungakhethi ii-divs zokuqala ezimbini ngu


driver.findElement(By.cssSelector('div[class*=calendar-day-]:not([class*='unavailable'])'));'

Ukufumana isiQinisekiso soMntwana

Ukufumana ithegi yomfanekiso, sisebenzisa:

driver.findElement(By.cssSelector('div#logo img'));

Izinto ezininzi zaBantwana

Kukho amaxesha apho kukho izinto ezininzi zabantwana ngaphakathi kwento enye yomzali enje ngoluhlu lwezinto


  • Apple

  • Orange

  • Banana

Njengoko kunokubonwa, uluhlu lwezinto ezizezinye azinayo nayiphi na i-id enxulumene nazo. Ukufumana into ngombhalo 'orenji', kuya kufuneka sisebenzise nth-of-type.

Umzekelo:


driver.findElement(By.cssSelector('ul#fruit li:nth-of-type(2)'));

Ngokunjalo, ukukhetha into yokugqibela yomntwana, okt 'iBhanana', sisebenzisa:

driver.findElement(By.cssSelector('ul#fruit li:last-child'));

Ii-Id ezinamandla

Sinokusebenzisa umtya wokulinganisa ukufumana izinto ezinee-ID ezinamandla.

Kule mzekelo, zonke izinto zontathu ze-div ziqulathe igama elithi 'random'.

Nika ukuqala nge

Ukukhetha eyokuqala div element, sisebenzisa ^= Oko kuthetha ukuba 'kuqala nge':

driver.findElement(By.cssSelector('div[id^='123']'));

Inqaku liphela nge

Ukukhetha yesibini div element, sisebenzisa $= Oko kukuthi 'iphela ngo':

driver.findElement(By.cssSelector('div[id$='456']'));

Nika iziqulatho

Ukukhetha eyokugqibela div element siza kuyisebenzisa *= Oko kuthetha ukuba 'umtya ongaphantsi'

driver.findElement(By.cssSelector('div[id*='_pattern_']'));

Sinokusebenzisa kwakhona contains

driver.findElement(By.cssSelector('div:contains('_pattern_')'));

Ukuqhubeka kokufunda: