1

I am new to Selenium. Not sure how to handle this scenario. I am working on a website which has several buttons with following code,

<a class="Some big class name" datacommunication="SelectItem" token="some token number" model-id="Id1" element="button">
<i class="classname">Book Ticket</i>
</a>

<a class="Some big class name" datacommunication="SelectItem" token="some token number" model-id="Id2" element="button">
<i class="classname">Book Ticket</i>
</a>

I tried to click on it using following commands, ele = driver.FindElement(By.ClassName("Some big class name")); but it fails with following message, Compound class names are not supported. Consider searching for one class name and filtering the results.

ele = driver.FindElement(By.CssSelector("a[model-id='Id1']")); fails with 'Test method TestBot.HomeTest.bookTicket threw exception: 
OpenQA.Selenium.WebDriverTimeoutException: Timed out after 10 seconds'

Tried using XPATH,

ele = driver.FindElement(By.XPath("\\\a[@model-id='Id1']")); doesn't work either.

I have no control on html. Can't change it.

Please let me know how to identify elements in such scenarios.

Amit
  • 25
  • 1
  • 2
  • 8
  • Which web driver are you using? Have you tried searching for something simple, i.e. (By.CssSelector("a")), just to make sure the DOM is loading correctly? (Note: the XPath should be "//a[@model-id='Id1']", but that won't help if the element isn't there). – bornfromanegg Jan 27 '15 at 16:44
  • You should try reading more into xpath and css_selectors. They allow you to find anything on the page. I use a lot of "contains()" or "text()" to find whatever I want on the screen. – IamBatman Aug 29 '17 at 22:17

6 Answers6

4

You can't have spaces in class names. Those are actually multiple classes separated by a space. You can find the above elements using a css selector

var ele = driver.FindElements(By.CssSelector(".Some.big.class.name"))

Of course, this will find both elements. To find just the first, you could use

var ele = driver.FindElement(By.CssSelector("a[model-id='Id1']"))

You can find help on css selectors here: http://www.w3schools.com/cssref/css_selectors.asp

Update:

I just noticed your XPath appears to have the slashes the wrong way around. If you wish to use XPath, try

//a[@model-id='Id1']

Note, however, that css selectors perform better than XPath.

bornfromanegg
  • 2,826
  • 5
  • 24
  • 40
1

There are multiple number of ways to locate your WebElement in Selenium WebDriver. But always remember all are based on you attribute or combination of HTML tags so case could be any of them 1- First way is using id 2- 2nd one is Name 3- Class Name 4- Some time you can used Tagname 5- Some time linkText 6- Some time partial link text 7- Using xpath 8- Using css selector

So in you case we need to take help of Xpath and Css Selector So xpath of you elements

Syntax :

//[@attribute ='value of selected tag']
Example id1:
//a[@model-id='Id1']
id2:
//a[@model-id='Id2']

For both element following are the css Selector Syntax

 [attribute ='value']
id1:
a[model-id='Id1']

id2:

 a[model-id='Id2']

http://www.slideshare.net/weekendtesting/css-selector-29312437 http://www.slideshare.net/weekendtesting/locators-in-selenium-bnt-09

0

there is difference between findElements and findElement.

FindElement: findElement returns a single element.

FindElements : returns a list of same element.As in this example there are multiple classes with same class name , so use driver.findElements method .

driver.findElements will return a list of all elements with that class name . Now , you have list of all elements but you want only one of the element. So iterate over list to get a single element out of a list.

List<WebElement> elementList= driver.FindElement(By.ClassName("Some.big.class.name"));

Iterator itr = elementList.iterator();

while(itr.hasNext()) 

 {
     WebElement element = itr.next();

      if(element.getAttribute("model-id").equals("Id1")){
          element.click();
          break;
        }//if block ends here
   }//while loop ends here

You can also use XPATH , if nothing works

NarendraR
  • 7,577
  • 10
  • 44
  • 82
Deen John
  • 3,522
  • 4
  • 29
  • 32
  • Note that class="Some big class name" declares an element with *four* classes: "Some", "big", "class", and "name". It is *not* a single class "Some big class name". – bornfromanegg Jan 25 '15 at 10:25
  • That still won't work, I'm afraid - By.ClassName does not support compound class names. See http://stackoverflow.com/questions/20361643/compound-class-names-are-not-supported-consider-searching-for-one-class-name-an. I would still recommend using a css selector. – bornfromanegg Jan 26 '15 at 09:46
0

Thanks a lot for help. I have used following code to overcome above mentioned issue,

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a[data-model-id='c5']"))).Click();

With above code, I am able to click on the button.

Thanks again for your help and knowledge sharing.

Amit

Amit
  • 25
  • 1
  • 2
  • 8
0

You can locate by using xpath.

WebElement ele = driver.findElement(By.xpath("//*[@class='Some big class name']"));
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
SeemaS
  • 1
  • 2
-2

To identify the elements in selenium there are multiple ways.

To see the details please refer BY Class.

Try to find the way which can identify the element uniquely. Start with id if available and if nothing works go for XPATH. XPATH is slower than id and CSS selector.

h4k3r
  • 91
  • 5