i am using selenium java , and i want to click on one of the images and also i am not supposed to use xpath as it changes all the time , i am sharing my console error message and html code as well
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=103.0.5060.114)
For documentation on this error, please visit: https://selenium.dev/exceptions/#invalid_selector_exception
Build info: version: '4.3.0', revision: 'a4995e2c09*'
System info: host: 'AWAIS-PC', ip: '192.168.1.62', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '18.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [c2f759fa459cab68170d6765738976b5, findElement {using=css selector, value=img[@alt='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton ']}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 103.0.5060.114, chrome: {chromedriverVersion: 103.0.5060.53 (a1711811edd7..., userDataDir: C:\Users\WRP\AppData\Local\...}, goog:chromeOptions: {debuggerAddress: localhost:55963}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:55963/devtoo..., se:cdpVersion: 103.0.5060.114, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: c2f759fa459cab68170d6765738976b5
my script is
waits.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("img[@alt='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton ']")));
driver.findElement(By.cssSelector("img[@alt='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton ']")).click();
CodePudding user response:
You should use XPath:
waits.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//img[@alt='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton ']")));
driver.findElement(By.xpath("//img[@alt='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton ']")).click();
CodePudding user response:
You have provided @
with attribute name, when you are using css selector
@
is not required with attribute name.That's the reason you are getting following error An invalid or illegal selector was specified
You should use below two options: one with exact match and the other starts-with using css selector.
Option 1:
waits.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("img[alt='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton ']")));
driver.findElement(By.cssSelector("img[alt='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton ']")).click();
Option 2 : Starts-with
waits.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("img[alt^='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton']")));
driver.findElement(By.cssSelector("img[alt^='hover state of Hibiscus Camo Arch Logo T-shirt in Cotton']")).click();