如何使用Python Selenium从HTML DOM打印更多链接?

发布于 2025-01-22 13:29:14 字数 565 浏览 0 评论 0原文

html:

<div class="xxxx">
<a href="ooooo.pdf"></a>
</div>

Python Selenium代码试验:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
print(wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.xxxx a"))).get_attribute('href'))

输出:

aaaaa.pdf

如何打印OOOOO.pdfaaaaaa.pdf

我想打印更多链接,该怎么办?

Html:

<div class="xxxx">
<a href="ooooo.pdf"></a>
</div>

Python selenium code trials:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
print(wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.xxxx a"))).get_attribute('href'))

Output:

aaaaa.pdf

How print ooooo.pdf and aaaaa.pdf?

I want to print more links, what should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2025-01-29 13:29:14

element_to_be_be_clickable() 仅印刷第一个匹配元素的属性。


提取

所有 href 属性值您必须诱导 webdriverwait for visibility_of_All_ELEments_Located() ,您可以使用以下任何一个以下任何一个://stackoverflow.com/questions/48369043/official-locator-strategies-for for-the-webdriver“> 定位器策略

  • 使用

    使用 css_selector

      print([my_elem.get_attribute(“ href”)在webdriverwait中for my_elem(driver,10).until(ec.visibility_of_all_elements_located(((by.css_selector,by.css_selector,“ div.xxxx a”))))))
     
  • 使用 xpath

      print([[my_elem.get_attribute(“ href”)for webdriverwait中的my_elem(driver,20).until(ec.visibility_of_all_elements_located((by.xpath) //一个”)))])
     

替代方案

您也可以尝试:

  • 使用 css_selector

      print([my_elem.get_attribute(“ href”)for driver.find_elements中的my_elem(by.css_selector,“ div.xxxx a”)])
     
  • 使用 xpath

      print([my_elem.get_attribute(“ href”)for driver.find_elements中的my_elem(by.xpath,“ // div [@class ='xxxx'] //
     

element_to_be_clickable() returns a single WebElement hence href attribute of only the first matching element is printed.


Solution

To extract all the href attribute values you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.xxxx a")))])
    
  • Using XPATH:

    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='xxxx']//a")))])
    

Alternative

As an alternative you can also try:

  • Using CSS_SELECTOR:

    print([my_elem.get_attribute("href") for my_elem in driver.find_elements(By.CSS_SELECTOR, "div.xxxx a")])
    
  • Using XPATH:

    print([my_elem.get_attribute("href") for my_elem in driver.find_elements(By.XPATH, "//div[@class='xxxx']//a")])
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文