Skip to content

Commit 4532dc6

Browse files
committed
Updating .NET bindings to enable legacy behavior for spec-compliant drivers
1 parent f2c0cf5 commit 4532dc6

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

dotnet/src/webdriver/Remote/RemoteWebDriver.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,17 @@ public ReadOnlyCollection<IWebElement> FindElementsById(string id)
567567
{
568568
if (this.IsSpecificationCompliant)
569569
{
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);
571581
}
572582

573583
return this.FindElements("id", id);
@@ -594,7 +604,17 @@ public IWebElement FindElementByClassName(string className)
594604
// return this.FindElement("css selector", "." + className);
595605
if (this.IsSpecificationCompliant)
596606
{
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);
598618
}
599619

600620
return this.FindElement("class name", className);
@@ -621,7 +641,17 @@ public ReadOnlyCollection<IWebElement> FindElementsByClassName(string className)
621641
// return this.FindElements("css selector", "." + className);
622642
if (this.IsSpecificationCompliant)
623643
{
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);
625655
}
626656

627657
return this.FindElements("class name", className);

dotnet/test/common/ElementFindingTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void ShouldNotBeAbleToLocateByIdMultipleElementsThatDoNotExist()
5151
public void FindingASingleElementByEmptyIdShouldThrow()
5252
{
5353
driver.Url = formsPage;
54-
Assert.Throws<NoSuchElementException>(() => driver.FindElement(By.Id("")));
54+
Assert.Throws(Is.InstanceOf<NoSuchElementException>(), () => driver.FindElement(By.Id("")));
5555
}
5656

5757
[Test]

0 commit comments

Comments
 (0)