I am trying to find an element by using Selenium with python, by a known value of src attribute. The element is the point picture on the left of the address of a location in Google Maps.
Namely the html for the element I'm trying to select:
<img alt="" jstcache="935" src="//www.gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp.png" jsan="7.Liguzb,0.alt,8.src">
How can I select the given element by searching for it by using the link:
www.gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp.png
Thanks.
CodePudding user response:
To locate the element as the value of src
attribute is know to you, you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "img.Liguzb[src*='gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp']")
Using xpath:
element = driver.find_element(By.XPATH, "//img[@class='Liguzb' and contains(@src, 'gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp')]")
To locate a visible element instead of presence_of_element_located() you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img.Liguzb[src*='gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//img[@class='Liguzb' and contains(@src, 'gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp')]")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC