@@ -155,34 +155,40 @@ private Session GetSession(string sessionId)
155155
156156 private ( string strategy , string value ) ParseCssSelector ( string cssSelector )
157157 {
158- if ( cssSelector . StartsWith ( "#" ) )
159- {
160- return ( "id" , cssSelector . Substring ( 1 ) ) ;
161- }
162- if ( cssSelector . StartsWith ( "." ) )
163- {
164- return ( "class name" , cssSelector . Substring ( 1 ) ) ;
165- }
166-
167- var nameMatch = Regex . Match ( cssSelector , @"\*?\[name\s*=\s*""(.+?)""\]" , RegexOptions . IgnoreCase ) ;
158+ var nameMatch = Regex . Match ( cssSelector , @"^\*?\[name\s*=\s*""(.+?)""\]$" , RegexOptions . IgnoreCase ) ;
168159 if ( nameMatch . Success )
169160 {
170161 return ( "name" , UnescapeCssValue ( nameMatch . Groups [ 1 ] . Value ) ) ;
171162 }
172-
173- var idMatch = Regex . Match ( cssSelector , @"\*?\[id\s*=\s*""(.+?)""\]" , RegexOptions . IgnoreCase ) ;
163+
164+ var idMatch = Regex . Match ( cssSelector , @"^#((?:[\w-]|\\[0-9a-fA-F]{1,6}\s?)+)$" ) ;
174165 if ( idMatch . Success )
175166 {
176167 return ( "id" , UnescapeCssValue ( idMatch . Groups [ 1 ] . Value ) ) ;
177168 }
178-
179- var classMatch = Regex . Match ( cssSelector , @"\*?\[class\s*=\s*""(.+?)""\]" , RegexOptions . IgnoreCase ) ;
169+
170+ var classMatch = Regex . Match ( cssSelector , @"^\.((?:[\w-]|\\[0-9a-fA-F]{1,6}\s?)+)$" ) ;
180171 if ( classMatch . Success )
181172 {
182173 return ( "class name" , UnescapeCssValue ( classMatch . Groups [ 1 ] . Value ) ) ;
183174 }
184175
185- return ( "name" , cssSelector ) ;
176+ var idAttrMatch = Regex . Match ( cssSelector , @"^\*?\[id\s*=\s*""(.+?)""\]$" , RegexOptions . IgnoreCase ) ;
177+ if ( idAttrMatch . Success )
178+ {
179+ return ( "id" , UnescapeCssValue ( idAttrMatch . Groups [ 1 ] . Value ) ) ;
180+ }
181+
182+ var classAttrMatch = Regex . Match ( cssSelector , @"^\*?\[class\s*=\s*""(.+?)""\]$" , RegexOptions . IgnoreCase ) ;
183+ if ( classAttrMatch . Success )
184+ {
185+ return ( "class name" , UnescapeCssValue ( classAttrMatch . Groups [ 1 ] . Value ) ) ;
186+ }
187+
188+ throw WebDriverResponseException . UnsupportedOperation (
189+ $ "CSS selector '{ cssSelector } ' is not supported. Only simple selectors are allowed: " +
190+ "#id, .className, [name=\" value\" ], [id=\" value\" ], [class=\" value\" ]"
191+ ) ;
186192 }
187193
188194 private string UnescapeCssValue ( string cssValue )
0 commit comments