如何使用 Selenium 和 Python 在 div 中定位值
How to locate the value inside a div using Selenium and Python
如何使用 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
尝试通过 xpath 访问:
/html/body/div[1]/div/div[2]/div[2]/div[3]/div/div[1]/div[1]
在 Chrome 控制台中突出显示了那部分:
<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">
不知道如何获取数据代码值。
尝试过css_selector:
div[data-id='product']
得到相同的行:
<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">
又不知道了。
要打印 data-code
属性的值,您必须引入 WebDriverWait for the and you can use either of the following :
使用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"))
使用 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"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
如何使用 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
尝试通过 xpath 访问:
/html/body/div[1]/div/div[2]/div[2]/div[3]/div/div[1]/div[1]
在 Chrome 控制台中突出显示了那部分:
<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">
不知道如何获取数据代码值。
尝试过css_selector:
div[data-id='product']
得到相同的行:
<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">
又不知道了。
要打印 data-code
属性的值,您必须引入 WebDriverWait for the
使用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"))
使用 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"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC