[JAVA] Remedy for Selenium InvalidSelectorException error

What to do if you are using Selenium and get an InvalidSelectorException error

One of the causes of the error is when trying to get an element with a space in the class name. For example, suppose you want to get an element whose class name is "class name" as follows.

python


driver.findElements(By.className("class name"));

In Selenium, there are spaces in attribute values such as class and id. If this is left as it is, an error will occur, so You can get the element by specifying the cssSelector element with a slightly different description method. The description format is

driver.findElement (By.cssSelector ("{ tag name} [{ attribute name} ='{ attribute value}'] ");

python


driver.findElement(By.cssSelector("div[class='class name']"));

You can get the element by describing the tag name, attribute name, and attribute value using cssSelector.

that's all.

Recommended Posts

Remedy for Selenium InvalidSelectorException error
JMX support for Selenium Grid
Remedy for "A server is already running." Error when running rails s
[Android] Cause and remedy for TransactionTooLargeException
[For beginners] Run Selenium in Java
Workaround for Xcode 12 "Double-quoted include" error
Tips for handling pseudo-elements in Selenium