Selenium CSS Selectors Mienzaniso

Kutsvaga zvinhu neCSS vanosarudza ndiyo nzira inosarudzika sezvo ichikurumidza uye inoverengwa kupfuura XPath.

Iyi dzidziso inopa mienzaniso yenzvimbo dzekutsvaga dandemutande zvinhu muSelenium uchishandisa CSS sarudza.

CSS Selectors neKupa

Ngatimbofungidzirai tine tag ine hunhu hunotevera [id, kirasi, zita, kukosha]

Iyo generic nzira yekuwana zvinhu nehunhu ndeiyi:

css = element_name[='']

Muenzaniso:

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

Id Attribute

MuCSS, tinogona kushandisa # notation yekusarudza id hunhu hwechinhu:



Muenzaniso:

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

Kirasi Inopa

Iwo iwo musimboti unogona kushandiswa kutsvaga zvinhu neyavo _ _ + _ | hunhu.

Isu tinoshandisa iyo class notation.

.Ongorora:Tora hanya zvakanyanya paunenge uchishandisa iyo . notation sezvo paigona kuve neakawanda ewebhu zvinhu pane iyo HTML sosi ine yakafanana kirasi hunhu.

Akawanda Hunhu

Dzimwe nguva panofanirwa kuve neicho chakanangana nemaitiro ekusarudza kuitira kuti uwane chinhu chakakodzera.

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

Iko kukosha kwechiratidziro kunogona kunge kuri 'hapana' kana 'block' zvinoenderana neayo ajax kufona. Mune ino mamiriro, isu tinofanirwa kutsvaga chinhu nekese kirasi uye maitiro.

Muenzaniso:

Ipa HAPANA chinhu chakakosha kukosha

MuWebDriver, unowana sei zvinhu zvine hunhu hune hunhu hwausingade kusarudza? Uyu CSS sarudza muenzaniso unoratidza kuti SEI kusarudzika neakasarudzika kukosha kukosha

Ngatiti iwe une zvinhu zvakawanda zvinogovana zvakafanana hunhu uye hunhu kukosha, asi zvimwe zvezvinhu izvo zvine zvimwe zvinokosheswa zvakanamatira kune kukosha. semuenzaniso:

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

Mune snippet iri pamusoro, tinoda kusarudza zuva riripo (kureva maviri ekupedzisira element)

Sezvinoonekwa, ese mana divs ane 'kalendari-zuva-' asi maviri ekutanga zvakare ane 'isingawanikwe' yatisingade.

Iyo CSS inosarudza Yekusasarudza maviri ekutanga ma div ari

div

Kuwana Mwana Element

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

Kuti tiwane tag yemufananidzo, tinoshandisa:

Akawanda Mwana Elements

Kune dzimwe nguva apo paine zvinhu zvakawanda zvevana mukati meiyo imwechete mubereki chinhu senge runyorwa zvinhu

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

Sezvinoonekwa, izvo zvemukati zvinyorwa hazvina kana id yakabatana nazvo. Kuti tiwane chinhu chine mavara 'Orange', isu tinofanirwa kushandisa


  • Apple

  • Orange

  • Banana
.

Muenzaniso:

nth-of-type

Saizvozvo, kusarudza chinhu chekupedzisira chemwana, i.e. 'Banana', tinoshandisa:

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

Idenically Yakagadzirwa Ids

Tinogona kushandisa tambo matcher kuti tiwane zvinhu zvine simba rakagadzirwa maId.

Mumuenzaniso uyu, zvese zvitatu div zvinhu zvine izwi rekuti 'zvisina kurongeka'.

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

Ipa Unotanga ne

Kusarudza yekutanga element, isu taizoshandisa div zvinoreva kuti 'zvinotanga na':

^=

Attribute Inopera ne

Kusarudza yechipiri driver.findElement(By.cssSelector('div[id^='123']')); element, isu taizoshandisa div zvinoreva kuti 'kupera ne':

$=

Ipa Zvine

Kusarudza yekupedzisira driver.findElement(By.cssSelector('div[id$='456']')); element yataizoshandisa div zvinoreva kuti 'sub-tambo'

*=

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

contains

Kuenderera mberi nekuverenga: