@@ -567,7 +567,17 @@ public ReadOnlyCollection<IWebElement> FindElementsById(string id)
567
567
{
568
568
if ( this . IsSpecificationCompliant )
569
569
{
570
- return this . FindElements ( "css selector" , "#" + EscapeCssSelector ( id ) ) ;
570
+ string selector = EscapeCssSelector ( id ) ;
571
+ if ( selector == string . Empty )
572
+ {
573
+ // Finding multiple elements with an empty ID will return
574
+ // an empty list. However, finding by a CSS selector of '#'
575
+ // throws an exception, even in the multiple elements case,
576
+ // which means we need to short-circuit that behavior.
577
+ return new List < IWebElement > ( ) . AsReadOnly ( ) ;
578
+ }
579
+
580
+ return this . FindElements ( "css selector" , "#" + selector ) ;
571
581
}
572
582
573
583
return this . FindElements ( "id" , id ) ;
@@ -594,7 +604,17 @@ public IWebElement FindElementByClassName(string className)
594
604
// return this.FindElement("css selector", "." + className);
595
605
if ( this . IsSpecificationCompliant )
596
606
{
597
- return this . FindElement ( "css selector" , "." + EscapeCssSelector ( className ) ) ;
607
+ string selector = EscapeCssSelector ( className ) ;
608
+ if ( selector . Contains ( " " ) )
609
+ {
610
+ // Finding elements by class name with whitespace is not allowed.
611
+ // However, converting the single class name to a valid CSS selector
612
+ // by prepending a '.' may result in a still-valid, but incorrect
613
+ // selector. Thus, we short-ciruit that behavior here.
614
+ throw new InvalidSelectorException ( "Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead." ) ;
615
+ }
616
+
617
+ return this . FindElement ( "css selector" , "." + selector ) ;
598
618
}
599
619
600
620
return this . FindElement ( "class name" , className ) ;
@@ -621,7 +641,17 @@ public ReadOnlyCollection<IWebElement> FindElementsByClassName(string className)
621
641
// return this.FindElements("css selector", "." + className);
622
642
if ( this . IsSpecificationCompliant )
623
643
{
624
- return this . FindElements ( "css selector" , "." + EscapeCssSelector ( className ) ) ;
644
+ string selector = EscapeCssSelector ( className ) ;
645
+ if ( selector . Contains ( " " ) )
646
+ {
647
+ // Finding elements by class name with whitespace is not allowed.
648
+ // However, converting the single class name to a valid CSS selector
649
+ // by prepending a '.' may result in a still-valid, but incorrect
650
+ // selector. Thus, we short-ciruit that behavior here.
651
+ throw new InvalidSelectorException ( "Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead." ) ;
652
+ }
653
+
654
+ return this . FindElements ( "css selector" , "." + selector ) ;
625
655
}
626
656
627
657
return this . FindElements ( "class name" , className ) ;
0 commit comments