我正在嘗試通過將 Selenium 與 python 結合使用,通過 src 屬性的已知值來查找元素。該元素是谷歌地圖中某個位置地址左側的點圖片。
即我要選擇的元素的html:
<img alt="" jstcache="935" src="//www.gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp.png" class="Liguzb" jsan="7.Liguzb,0.alt,8.src">
如何通過使用鏈接搜索給定元素來選擇它:
www.gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp.png
謝謝。
uj5u.com熱心網友回復:
要在您知道屬性值的情況下定位元素src,您可以使用以下任一定位器策略:
使用css_selector:
element = driver.find_element(By.CSS_SELECTOR, "img.Liguzb[src*='gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp']")使用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')]")
要定位可見元素而不是present_of_element_located () ,您需要為visibility_of_element_located()引入WebDriverWait,您可以使用以下任一定位器策略:
使用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']")))使用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')]")))注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433128.html
標籤:Python 硒 路径 css 选择器 网络驱动程序等待
