如何使用硒和Python在DIV内部定位值

发布于 2025-01-23 06:27:51 字数 1562 浏览 0 评论 0原文

如何使用XPATH \ CSS_SELECTOR在DIV内找到值,

我有此HTML:

<div class="catalog-products view-tile" data-catalog-products="" data-slider-available="" data-primary-as-icon=""><div data-id="product" class="catalog-product ui-button-widget" data-product="5a7b4c6d-0bc3-11ec-a2b0-00155dfc8232" data-code="4862447" data-preview-slider-inited="1"><div class="catalog-product__image"><a class="catalog-product__image-link" href="/product/5a7b4c6d0bc3c823/videokarta-msi-geforce-210-n210-1gd3lp/" data-toggle-slider=""><picture><source type="image/webp" media="(min-width: 768px)" etc

因此,我需要获取数据代码值 4862447

  1. 尝试通过XPATH访问:

     /html/hod/div [1]/div/div/div [2]/div [2]/div [3]/div/div/div [1]/div [1]
     

    在Chrome Console中突出显示该部分:

     &lt; div data-id =“ product” class =“ catalog-product ui button-widget” data-product =“ 5A7B4C6D-0BC3-11EC-A2B0-0015555555555555555555555555555555555555555555555555DFC8232” data Code =“ 4862447” 4862447“ 4862447”“ 4862447” data-preview-slider-inted =“ 1”&gt;
     

    不知道如何获得数据代码值。

  2. 尝试了CSS_SELECTOR:

      div [data-id ='product']
     

    获得相同的行:

     &lt; div data-id =“ product” class =“ catalog-product ui button-widget” data-product =“ 5A7B4C6D-0BC3-11EC-A2B0-0015555555555555555555555555555555555555555555555555DFC8232” data Code =“ 4862447” 4862447“ 4862447”“ 4862447” data-preview-slider-inted =“ 1”&gt;
     

    又不知道。

How to locate value inside a div using xpath \ css_selector

I have this html:

<div class="catalog-products view-tile" data-catalog-products="" data-slider-available="" data-primary-as-icon=""><div data-id="product" class="catalog-product ui-button-widget" data-product="5a7b4c6d-0bc3-11ec-a2b0-00155dfc8232" data-code="4862447" data-preview-slider-inited="1"><div class="catalog-product__image"><a class="catalog-product__image-link" href="/product/5a7b4c6d0bc3c823/videokarta-msi-geforce-210-n210-1gd3lp/" data-toggle-slider=""><picture><source type="image/webp" media="(min-width: 768px)" etc

So I need to get data-code value 4862447

  1. Tried to access via xpath:

    /html/body/div[1]/div/div[2]/div[2]/div[3]/div/div[1]/div[1]
    

    Got highlighted in Chrome console that part:

    <div data-id="product" class="catalog-product ui-button-widget" data-product="5a7b4c6d-0bc3-11ec-a2b0-00155dfc8232" data-code="4862447" data-preview-slider-inited="1">
    

    Don't know how to get data-code value.

  2. Tried css_selector:

    div[data-id='product']
    

    Got same line:

    <div data-id="product" class="catalog-product ui-button-widget" data-product="5a7b4c6d-0bc3-11ec-a2b0-00155dfc8232" data-code="4862447" data-preview-slider-inited="1">
    

    And no idea again.

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

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

发布评论

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

评论(1

我的奇迹 2025-01-30 06:27:51

要打印 data-code 属性的值,您必须诱导 webdriververiverwait < /a> for vistibility_of_element_located() ,您可以使用以下任何一个HREF =“ https://stackoverflow.com/a/48056120/7429447”> 定位器策略

  • 使用 css_selector ::

      print(webdriverwait(driver,20).until(ec.visibility_of_element_located((((by.css_selector) data-id ='product']”)))。get_attribute(“ data-code”))
     
  • 使用 xpath

      print(webdriverwait(驱动程序,20).until(ec.visibility_of_element_located(((by.xpath,“ // div [@class ='catalog-products view tile-tile'] ='product'和 @class ='catalog-product ui button-widget'] ).get_attribute(“数据代码”))
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

To print the value of the data-code attribute you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.catalog-products.view-tile > div.catalog-product.ui-button-widget[data-id='product']"))).get_attribute("data-code"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='catalog-products view-tile']/div[@data-id='product' and @class='catalog-product ui-button-widget'][.//a[@class='catalog-product__image-link' and @href and @data-toggle-slider]]"))).get_attribute("data-code"))
    
  • 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 和您的相关数据。
原文