How to access link of a list using an index or similar
I'm trying to learn some selenium webdriver using C#.
My problem: I wan't to access a list using an index.
HTML Code:
<ul class="pageNavigation">
<li><a href="/users/dashboard">» Dashboard</a></li>
<li><a href="/users">» Profile</a></li>
<li><a href="/accounts/settings">» Settings</a></li>
In WatiN i did this,
ie.List(Find.ByClass("pageNavigation")).ListItem(Find.ByIndex(2)).Links[0].Click();
how can Selenium do the same?
And I'm sorrry if this question is a bit basic开发者_运维百科.
Your xpath looks way too complicated. Btw, why do you want to access index? Try something like this or use By.partialText if you want
WebElement link = driver.findElement(By.linkText("Profile"));
link.click();
Second longer route would be
List<WebElement> links = driver.findElements(By.cssSelector("ul.pageNavigation>li>a"));
for(WebElement link:links){
String linkText = link.getText();
if(linkText.equals("oneIwanted")){
//do stuff
//get index and all
}
}
Step 1: Find CSS Selectors
css=.pageNavigation > a[href*='dashboard']
css=.pageNavigation > a[href='/users']
css=.pageNavigation > a[href*='settings']
Then C# API can be used to Perform Operations using above Locators.
精彩评论