Selenium Dzvanya Unganidzo Na href Kukosha

MuSelenium WebDriver, pane nzira dzinoverengeka dzatinogona kudyidzana nezvinhu zvewebhu, senge ID yechinhu, XPath, CSS, nezvimwe…

Tinogona zvakare kubaya zvinongedzo ne linkText kana partialLinkText. Idzi nzira dzakanaka kana tichiziva chinyorwa chinotarisirwa pakati pe ma tag.

Nekudaro kana isu tichiita zvemunharaunda kana kuyedza kwepasirese, iwo mavara anodudzirwa zvinoenderana uye hatichakwanise kushandisa linkText kana chidimbuLinkText, kunyanya kana ma tag anchor asina chero ID kana kirasi.

Ngatitii tinoda kubaya chinongedzo chinonongedzera ku profile.html,

semuenzaniso

View Profile

Sezvambotaurwa, tinogona kushandisa



driver.findElement(By.linkText('View Profile')).click()

uye izvo zvinoshanda, sekureba sekunge isu tiri papeji ine mutauro Chirungu.

Kana isu tikachinja mutauro kuenda kuGerman, semuenzaniso, yedu link ikozvino inoratidza

Profil ansehen

Zvino, mutsetse

driver.findElement(By.linkText('View Profile')).click()

zvaizotadza nekuti hapana chinongedzo pane peji remberi neicho chinyorwa.

Imwe nzira yekukunda izvi kudzvanya chinongedzo neayo href value, nekuti kunyangwe kana mutauro wesaiti ukachinja href chinongedzo chinofanira kunongedza kunzvimbo imwechete.

Mune mamwe mazwi, kudyidzana kwepasi rose hakufanirwe kukanganisa href yemubatanidzwa.

WebDriver haina yakazvimirira uye yakanangana nzira yekudzvanya chinongedzo neayo href kukosha. Panzvimbo iyoyo, isu tinofanirwa kutora ese maratidziro ari mune peji, tora href hunhu uye tobva taenzanisa iyo href kukosha nezvatinotarisira.

Dzvanya Chibatanidzo Na href

Ngatitii tine zvinotevera zvinongedzo uye isu tinoda kudzvanya pane iyo link link

View Profile Transactions public void clickLinkByHref(String href) {
List anchors = driver.findElements(By.tagName('a');
Iterator i = anchors.iterator();
while(i.hasNext()) {
WebElement anchor = i.next();
if(anchor.getAttribute('href').contains(href)) {

anchor.click();

break;
}
} }

Ipapo tinogona kushandisa clickLinkByHref('Profile')

uye ipapo iro riri pamusoro basa rinowana ese maratidziro ari papeji uye iterate kuburikidza navo uye kana yawana chinongedzo chine mbiri, WebDriver inodzvanya chinongedzo.

Ziva, kana isu tiine dzinoverengeka zvinongedzo neshoko rezita mavari, iro riri pamusoro basa rinogara richingobaya yekutanga link ine izwi rezita. Tinogona kushandura iyi kodhi iri pamusoro kuti tibatanidze kuti ndeapi ma link atinoda kudzvanya:

public void clickLinkByHref(String href, int position) {
List anchors = driver.findElements(By.tagName('a');
Iterator i = anchors.iterator();
int j = 0;
while(i.hasNext()) {
WebElement anchor = i.next();

if(anchor.getAttribute('href').contains(href)) {

j++;
}

if(anchor.getAttribute('href').contains(href)


&& j == position) {

anchor.click();

break;
}
} }

Kuenderera mberi nekuverenga: