The following piece of code runs without error:
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = ChromeOptions()
options.add_argument("--headless")
selectors = [
'img[alt=Google]',
'img[alt="Google"]'
]
with webdriver.Chrome(options=options) as driver:
driver.get("https://www.google.com")
wait = WebDriverWait(driver, 5)
for selector in selectors:
t = By.CSS_SELECTOR, selector
wait.until(EC.presence_of_element_located(t))
Note the two slightly different forms of CSS selector.
I have always understood that the definitive syntax for this would be:
'img[alt="Google"]'
Note the double-quotes.
I have determined that in selenium 4.22.0 (Python 3.12.4) the following syntax also works:
'img[alt=Google]'
Not the absence of double-quotes.
So my question is whether leaving out the double-quotes complies with CSS standards or is this something peculiar to selenium?
2
With thanks to @David (who found documentation that eluded me) it seems that the attribute value must be quoted if it contains spaces or special characters. See here