如何使用Selenium提取属性图片列表的值Python
How to extract the value of the attribute picture list using Selenium Python
我有以下代码
<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代码可以获得一条属性图记录,但这是动态的,有时我有一个、两个、三个、十二个或五十个。
如何通过具有动态列表大小的 xpath sl-video-preview 获取所有出现的属性值。
这个答案在Java。
// Find All elements at once
list<WebElement> pictureURLs = driver.findElements(By.xpath("//div[@class='gallery-
list']//a/picture"));
//Loop through the element list & print the required attributes
for(WebElement pictureurl:pictureURLs)
{
System.out.println(pictureurl..get_attribute("sl-video-preview"));
}
您需要修改您的函数以使用 find_elements_by_xpath which returns a List of WebElements 作为当前代码 returns 只有与提供的定位器匹配的单个(第一个)WebElement。
建议的代码更改:
pictures = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//picture")))
for picture in pictures:
print(picture.get_attribute('sl-video-preview'))
展望未来,您可以考虑重构代码以始终使用 Page Object Model Design Pattern, this way you can get benefit from lazy initialization approach and will not have to define Waits。
看来你很接近。对于 <figure>
节点,您已经考虑了 类 figure
和 hd
但它们都没有,您需要遍历它们。此外,您需要使用 visibility_of_all_elements_located()
而不是 visibility_of_element_located()
,并且您可以使用以下任一解决方案:
使用CSS_SELECTOR
:
print([my_elem.get_attribute("sl-video-preview") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.gallery-list figure.figure picture.ng-isolate-scope.sl-safe")))])
使用XPATH
:
print([my_elem.get_attribute("sl-video-preview") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='gallery-list']//figure[contains(@class, 'figure')]//picture[@class='ng-isolate-scope sl-safe']")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我有以下代码
<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代码可以获得一条属性图记录,但这是动态的,有时我有一个、两个、三个、十二个或五十个。 如何通过具有动态列表大小的 xpath sl-video-preview 获取所有出现的属性值。
这个答案在Java。
// Find All elements at once
list<WebElement> pictureURLs = driver.findElements(By.xpath("//div[@class='gallery-
list']//a/picture"));
//Loop through the element list & print the required attributes
for(WebElement pictureurl:pictureURLs)
{
System.out.println(pictureurl..get_attribute("sl-video-preview"));
}
您需要修改您的函数以使用 find_elements_by_xpath which returns a List of WebElements 作为当前代码 returns 只有与提供的定位器匹配的单个(第一个)WebElement。
建议的代码更改:
pictures = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//picture")))
for picture in pictures:
print(picture.get_attribute('sl-video-preview'))
展望未来,您可以考虑重构代码以始终使用 Page Object Model Design Pattern, this way you can get benefit from lazy initialization approach and will not have to define Waits。
看来你很接近。对于 <figure>
节点,您已经考虑了 类 figure
和 hd
但它们都没有,您需要遍历它们。此外,您需要使用 visibility_of_all_elements_located()
而不是 visibility_of_element_located()
,并且您可以使用以下任一解决方案:
使用
CSS_SELECTOR
:print([my_elem.get_attribute("sl-video-preview") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.gallery-list figure.figure picture.ng-isolate-scope.sl-safe")))])
使用
XPATH
:print([my_elem.get_attribute("sl-video-preview") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='gallery-list']//figure[contains(@class, 'figure')]//picture[@class='ng-isolate-scope sl-safe']")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC