开发者

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">&#187; Dashboard</a></li>
  <li><a href="/users">&#187; Profile</a></li>
  <li><a href="/accounts/settings">&#187; 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜