作为测试的一部分,我一直在寻找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 有些可以帮助我们。 谢谢
答案 0 :(得分:1)
答案 1 :(得分:0)
要提取属性 sl-video-preview
的值,由于该元素是Angular元素,因此您必须为{{引出 WebDriverWait 1}},则可以使用以下任何Locator Strategies:
使用visibility_of_element_located()
:
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"))
答案 2 :(得分:0)
我有问题,为什么在某些情况下标记图会多次出现,而@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"))
如何获取所有记录?