簡體   English   中英

使用 img 的任何可能的屬性獲取由 img 定位的元素

[英]get element located by img using any of its attributes which ever is possible

我正在使用 selenium java,我想單擊其中一張圖像,而且我不應該使用 xpath,因為它一直在變化,我也在分享我的控制台錯誤消息和 html 代碼

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

我的腳本是

 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();

您應該使用 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();

您提供了帶有屬性名稱的@ ,當您使用css selector時,屬性名稱不需要@ 。這就是您收到以下錯誤的原因An invalid or illegal selector was specified

您應該使用以下兩個選項:一個是完全匹配的,另一個是使用 css 選擇器開始的。

選項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();

選項 2:開始於

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM