如何使用Selenium提取属性图片的值Python
How to extract the value of the attribute picture using Selenium Python
作为测试的一部分,我一直在寻找 XPath 代码来获取 HTML 元素的属性值。
<div class="gallery-list">
<figure class="figure hd" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<div>
我需要通过 xpath 获取属性值 sl-video-preview
有些可以帮助我们。
谢谢
这是您可以使用的通用 xpath。
//figure[@class='figure hd']/picture
并且您必须获得 'sl-video-preview' 属性。
Chrome 控制台输出:
提取 属性 sl-video-preview
的值,因为元素是 Angular element you have to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following :
使用CSS_SELECTOR
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.gallery-list > figure.figure.hd > a > picture.ng-isolate-scope.sl-safe"))).get_attribute("sl-video-preview"))
使用XPATH
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='gallery-list']/figure[@class='figure hd']/a/picture[@class='ng-isolate-scope sl-safe']"))).get_attribute("sl-video-preview"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我有问题,为什么在某些情况下标签图出现多次而@DebanjanB 的这个例子只得到一条记录,那么我需要所有结果。
<div class="gallery-list">
<figure class="figure" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<figure class="figure hd" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<figure class="figure" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<div>
有效的 XPath 示例:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='gallery-list']/figure[@class='figure hd']/a/picture[@class='ng-isolate-scope sl-safe']"))).get_attribute("sl-video-preview"))
如何获取所有记录?
作为测试的一部分,我一直在寻找 XPath 代码来获取 HTML 元素的属性值。
<div class="gallery-list">
<figure class="figure hd" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<div>
我需要通过 xpath 获取属性值 sl-video-preview 有些可以帮助我们。 谢谢
这是您可以使用的通用 xpath。
//figure[@class='figure hd']/picture
并且您必须获得 'sl-video-preview' 属性。
Chrome 控制台输出:
提取 属性 sl-video-preview
的值,因为元素是 Angular element you have to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following
使用
CSS_SELECTOR
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.gallery-list > figure.figure.hd > a > picture.ng-isolate-scope.sl-safe"))).get_attribute("sl-video-preview"))
使用
XPATH
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='gallery-list']/figure[@class='figure hd']/a/picture[@class='ng-isolate-scope sl-safe']"))).get_attribute("sl-video-preview"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
我有问题,为什么在某些情况下标签图出现多次而@DebanjanB 的这个例子只得到一条记录,那么我需要所有结果。
<div class="gallery-list">
<figure class="figure" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<figure class="figure hd" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<figure class="figure" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<div>
有效的 XPath 示例:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='gallery-list']/figure[@class='figure hd']/a/picture[@class='ng-isolate-scope sl-safe']"))).get_attribute("sl-video-preview"))
如何获取所有记录?