如何使用硒和Python在DIV内部定位值
如何使用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/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;
不知道如何获得数据代码值。
尝试了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
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要打印
data-code
属性的值,您必须诱导 webdriververiverwait < /a> for vistibility_of_element_located() ,您可以使用以下任何一个HREF =“ https://stackoverflow.com/a/48056120/7429447”> 定位器策略 :使用 css_selector ::
使用 xpath :
注意:您必须添加以下导入:
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:
Using XPATH:
Note : You have to add the following imports :