How would one locate following object [ul, li] in selenium webdriver?

时间:2015-04-23 05:14:57

标签: selenium

Could you help me in figuring out how to proceed with locating "new" using selenium webdriver in Java.

Code Snippet:

<ul class="ms-crm-CommandBar-Menu"> 
<li tabindex="-1" class="ms-crm-CommandBarItem ms-crm-CommandBar-Menu ms-crm-CommandBar-Button" title="New Create a new Account record." id="account|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.account.NewRecord" command="account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid" style="white-space: pre-line; display: inline-block;"><span tabindex="-1" class="ms-crm-CommandBar-Button ms-crm-Menu-Label" style="max-width:200px"><a tabindex="0" class="ms-crm-Menu-Label" onclick="return false"><img tabindex="-1" class="ms-crm-ImageStrip-New_16 ms-crm-commandbar-image16by16" src="/_imgs/imagestrips/transparent_spacer.gif" style="vertical-align:top" alt="New"> <span tabindex="-1" class="ms-crm-CommandBar-Menu" style="max-width:150px" command="account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid"> New </span> </a> </span> </li>

1 个答案:

答案 0 :(得分:0)

Disclaimer: I'm using C# but shouldn't make much difference.

The easiest would be using a CssSelector. It's a bit verbose in this case, but that doesn't matter:

driver.FindElement(By.CssSelector("span[command=\"account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid\"]"));

This will find the span element which contains nothing but the text you want. Assuming the command is unique on the page, otherwise you'll need to refine.

Alternatively you can use XPath to achieve the same:

driver.FindElement(By.XPath("//span[@command='account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid']");
相关问题