如何使用 Selenium Python 通过已知 src 属性查找元素

发布于 2025-01-09 10:59:28 字数 425 浏览 0 评论 0原文

我试图通过使用 Selenium 和 python,通过 src 属性的已知值来查找元素。该元素是 Google 地图中某个位置的地址左侧的点图片。

即我尝试选择的元素的 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

谢谢。

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" class="Liguzb" 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2025-01-16 10:59:28

要定位元素,因为您知道 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' )]")
    

定位可见元素而不是presence_of_element_ located()你需要诱导WebDriverWaitvisibility_of_element_ located( ) 并且您可以使用以下任一定位器策略

  • 使用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/图标/材质/system_gm/1x/place_gm_blue_24dp')]")))
    
  • 注意:您必须添加以下导入:

    从 selenium.webdriver.support.ui 导入 WebDriverWait
    从 selenium.webdriver.common.by 导入
    从 selenium.webdriver.support 导入预期条件作为 EC
    

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
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文