I have a WebElement and I'm simply trying to extract the CSS selector string. This is the value of the variable when I debug the code:
[[ChromeDriver: chrome on MAC (345345345n5435345b34)] -> css selector:
div[class='alert alert-danger']]
I need to have access to just the css selector:
div[class='alert alert-danger']]
I could split the string, but does anyone know of a more efficient method that Selenium provides? I've tried using .getAttribute("class") which returns "alert alert-danger" but I need the entire selector.
Thank you for reading, any help would be much appreciated.
解决方案
One simple way would be to use regex. From the given string you provided you could extract the whole selector by using the following regex expression:
div.*
Sometimes you have other elements (not always div), so you could create a method which takes this as an argument, so you can use the same for for example: ul.* etc.