如何使用Selenium Python找到HREF属性

发布于 2025-01-23 19:40:40 字数 1317 浏览 0 评论 0原文

我尝试了许多网络解决方案,但找不到如何找到HREF。

我的代码之一:

driver = webdriver.Chrome(executable_path='C:\chromedriver.exe')
driver.get(("https://cordis.europa.eu/search/fr?q=contenttype%3D%27project%27%20AND%20programme%2Fcode%3D%27H2020%27&p=1&num=100&srt=/project/contentUpdateDate:decreasing"))

time.sleep(20)
driver.execute_script("window.scrollTo(0,10000)")

elems = driver.find_elements(By.CSS_SELECTOR, ".c-card-search__title.ng-star-inserted [href]")
links = [elem.get_attribute('href') for elem in elems]

网站: https://cordis.europa.eu/search/search/fr?q=contentType%3DDPROXPROJECT; 20个程序为2fcode%3D%27H2020%27& p = 1& num = 10& srt =/project/contentUpdateDate:减少

insepctor:

<a _ngcontent-vtw-c99="" class="c-card-search__title ng-star-inserted" href="/project/id/881603"> Graphene Flagship Core Project 3 </a>

I tried many of the web solution but I can't find how to find the HREF.

One of my code:

driver = webdriver.Chrome(executable_path='C:\chromedriver.exe')
driver.get(("https://cordis.europa.eu/search/fr?q=contenttype%3D%27project%27%20AND%20programme%2Fcode%3D%27H2020%27&p=1&num=100&srt=/project/contentUpdateDate:decreasing"))

time.sleep(20)
driver.execute_script("window.scrollTo(0,10000)")

elems = driver.find_elements(By.CSS_SELECTOR, ".c-card-search__title.ng-star-inserted [href]")
links = [elem.get_attribute('href') for elem in elems]

The web site: https://cordis.europa.eu/search/fr?q=contenttype%3D%27project%27%20AND%20programme%2Fcode%3D%27H2020%27&p=1&num=10&srt=/project/contentUpdateDate:decreasing

The insepctor:

<a _ngcontent-vtw-c99="" class="c-card-search__title ng-star-inserted" href="/project/id/881603"> Graphene Flagship Core Project 3 </a>

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

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

发布评论

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

评论(2

安稳善良 2025-01-30 19:40:40

href 属性在以下 &lt; a&gt; nodes中。

<a _ngcontent-jdx-c99="" class="c-card-search__title ng-star-inserted" href="/project/id/881603"> Graphene Flagship Core Project 3 </a>

解决方案,

打印 href 属性的值的 您必须诱导 a> for vineibility_of_all_elements_located() https://stackoverflow.com/questions/48369043/48369043/official-locator-strategies-for-the-webdriver"> loc> loc> loc> loc> loc> loc>

  • 使用 css_selector使用 >:

     驱动程序; num = 100&amp; srt =/project/contentupdatedate:减少“)
    webdriverwait(驱动程序,20).unifor(ec.element_to_be_clickable(((by.xpath,“ // button [@class ='o-btn o-btn-large o-btn-large o-btn--light cookie-cookie-cookeent___-light cookie-consent__button __button) -consent__button- hall'])))。单击()
    print([my_elem.get_attribute(“ href”)for webDriverWait中的my_elem(driver,20).until(ec.visibility_of_all_elements_located(((by.css_selector,“ Ac-card-search__title.ng-stitle.ng-star-star-star-inserted [href)) )))
     

  • 使用 xpath

     驱动程序; num = 100&amp; srt =/project/contentupdatedate:减少“)
    webdriverwait(驱动程序,20).unifor(ec.element_to_be_clickable(((by.xpath,“ // button [@class ='o-btn o-btn-large o-btn-large o-btn--light cookie-cookie-cookeent___-light cookie-consent__button __button) -consent__button- hall'])))。单击()
    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='c-card-search__title ng-star-插入'and @href]”))))))))
     
  • 控制台输出:

      ['https://cordis.europa.eu/project/project/ID/881603','https://cordis.europa.eu/project/project/project/project/ID/945539','https:// cordis。 europa.eu/project/id/964827','https://cordis.europa.eu/project/project/project/ID/956396','https://cordis.europis.eu/proign/project/project/project/project/ID/ID/ID/956544',https: //cordis.europa.eu/project/id/101037031', 'https://cordis.europa.eu/project/id/101025100', 'https://cordis.europa.eu/project/id/101007165' ,'https://cordis.europa.eu/project/id/959234','https://cordis.europa.eu/project/project/project/ID/ID/952520',' ID/959879','https://cordis.europa.eu/project/project/ID/101005177','https://cordis.euuropa.eu/project/project/project/id/101004714' eu/project/id/946044','https://cordis.europa.eu/project/project/ID/961989','https://cordis.europa.eu/proptaimpt/propt/proignt/propt/proignt/ID/ID/ID/962483',httpps cordis.europa.eu/project/id/862011','https://cordis.europa.eu/project/project/project/ID/891402',' https://cordis.europa.eu/project/ID/870378','https://cordis.europa.eu/project-eu/project/project/project/ID/870743',' 834195','https://cordis.europa.eu/project/project/ID/852985','https://cordis.europa.eu/project/project/project/project/814837' project/id/861278','https://cordis.europa.eu/project/project/ID/842243','https://cordis.europa.eu/project/project/project/ID/ID/833507',833507','https:// cordis。 europa.eu/project/id/825496','https://cordis.europa.eu/project/project/project/ID/820350',' //cordis.europa.eu/project/ID/786890','https://cordis.europa.eu/project/project/project/project/824295',' ,'https://cordis.europa.eu/project/id/768960','https://cordis.europa.eu/projection/project/project/id/id/id/772337',' id/793965','https://cordis.europa.eu/project/project/ID/797942','https://cordis.europa.eu/project/project/project/id/757184','https://cordis.europa。 eu/project/id/639020','https://cordis.europa.eu/project/project/ID/694665','https://cordis.europa.eu/propta/proptiment/propt/propt/propt/id/id/id/id/id/679403','https:// https:// cordis.europa.eu/project/ID/677061','https://cordis.europa.eu/project/project/project/ID/ID/677493',',' https://cordis.europa.eu/project/ID/677823','https://cordis.europa.eu/project-eu/project/project/id/id/716472',' 721537','https://cordis.europa.eu/project/project/ID/725627','https://cordis.europa.eu/projecn/project/project/694343' project/id/677650','https://cordis.europa.eu/project/project/ID/678423','https://cordis.europa.eu/project/project/project/id/id/ID/679243','https:// cordis。 uropa.eu/project/id/846617','https://cordis.europa.eu/project/project/project/ID/101002761','https://cordis.europis.eu/propta.eu/project/project/project/project/project/ID/ID/ID/8448325',https:https:https: //cordis.europa.eu/project/ID/850488','https://cordis.europa.eu/project/project/project/854931','https://cordis.europis.eu/propa/proign/proignt/project/project/ID/ID/10102277373737''' ,'https://cordis.europa.eu/project/id/id/101030066','https://cordis.europa.eu/project/project/project/id/id/101020016',' id/101024758','https://cordis.europa.eu/project/project/ID/850769','https://cordis.euupa.eu/project/project/project/ID/863227' eu/project/id/861910','https://cordis.europa.eu/project/project/ID/878074','https://cordis.europa.eu/propta/propt/propt/propt/propt/id/id/id/id/iid/875905','https:// https:// cordis.europa.eu/project/id/862111','https://cordis.europa.eu/project/project/project/ID/846793',',' https://cordis.europa.eu/project/ID/101003472','https://cordis.europa.eu/proign/project/project/project/ID/101036910',' 101036449','https://cordis.europa.eu/project/project/ID/101008571','https://cordis.europa.eu/project/project/project/project/ID/101018420' project/id/101037564','https://cordis.europa.eu/project/project/ID/101000622','https://cordis.europa.eu/propt/project/ID/ID/ID/101021936','https:// https:// cordis。 europa.eu/project/id/101037424','https://cordis.europa.eu/project/project/project/project/ID/101008233','https://cordis.europis.eu/propta.eu/project/project/project/ID/1010034700/1010034700','https:https:https:https: ///cordis.europa.eu/project/id/101008297','https://cordis.europa.eu/project/project/project/id/101003815','https://cordis.europa.euu/propta.eu/project/project/project/ID/ID/956355555555555555555555555555555555555555555555555555555555555555性调制号,'https://cordis.europa.eu/project/id/id/101003914','https://cordis.europa.eu/project/project/project/project/ids/ID/101017304',' ID/101007326','https://cordis.europa.eu/project/project/ID/101003491','https://cordis.euuropa.eu/proign/proign/proign/id/id/ID/101033819' eu/project/id/101016902','https://cordis.europa.eu/project/project/ID/101003826','https://cordis.europa.eu/project/project/project/ID/ID/ID/9555317',htttps cordis.europa.eu/project/id/101004633', 'https://cordis.europa.eu/project/id/872427', 'https://cordis.europa.eu/project/id/101006249', ' https://cordis.europa.eu/project/ID/101005122','https://cordis.europa.eu/project-eu/project/project/ID/ID/952851',' 855187','https://cordis.europa.eu/project/project/id/892429','https://cordis.europa.eu/project/project/project/project/865067'项目/ID/883520']
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

The href attributes are within the following <a> nodes.

<a _ngcontent-jdx-c99="" class="c-card-search__title ng-star-inserted" href="/project/id/881603"> Graphene Flagship Core Project 3 </a>

Solution

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

  • Using CSS_SELECTOR:

    driver.get("https://cordis.europa.eu/search/fr?q=contenttype%3D%27project%27%20AND%20programme%2Fcode%3D%27H2020%27&p=1&num=100&srt=/project/contentUpdateDate:decreasing")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='o-btn o-btn--large o-btn--light co-cookie-consent__button co-cookie-consent__button--allow']"))).click()
    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.c-card-search__title.ng-star-inserted[href]")))])
    
  • Using XPATH:

    driver.get("https://cordis.europa.eu/search/fr?q=contenttype%3D%27project%27%20AND%20programme%2Fcode%3D%27H2020%27&p=1&num=100&srt=/project/contentUpdateDate:decreasing")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='o-btn o-btn--large o-btn--light co-cookie-consent__button co-cookie-consent__button--allow']"))).click()
    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='c-card-search__title ng-star-inserted' and @href]")))])
    
  • Console Output:

    ['https://cordis.europa.eu/project/id/881603', 'https://cordis.europa.eu/project/id/945539', 'https://cordis.europa.eu/project/id/964827', 'https://cordis.europa.eu/project/id/956396', 'https://cordis.europa.eu/project/id/956544', 'https://cordis.europa.eu/project/id/101037031', 'https://cordis.europa.eu/project/id/101025100', 'https://cordis.europa.eu/project/id/101007165', 'https://cordis.europa.eu/project/id/959234', 'https://cordis.europa.eu/project/id/952520', 'https://cordis.europa.eu/project/id/959879', 'https://cordis.europa.eu/project/id/101005177', 'https://cordis.europa.eu/project/id/101004714', 'https://cordis.europa.eu/project/id/946044', 'https://cordis.europa.eu/project/id/961989', 'https://cordis.europa.eu/project/id/962483', 'https://cordis.europa.eu/project/id/862011', 'https://cordis.europa.eu/project/id/891402', 'https://cordis.europa.eu/project/id/886365', 'https://cordis.europa.eu/project/id/870378', 'https://cordis.europa.eu/project/id/870743', 'https://cordis.europa.eu/project/id/834195', 'https://cordis.europa.eu/project/id/852985', 'https://cordis.europa.eu/project/id/814837', 'https://cordis.europa.eu/project/id/861278', 'https://cordis.europa.eu/project/id/842243', 'https://cordis.europa.eu/project/id/833507', 'https://cordis.europa.eu/project/id/825496', 'https://cordis.europa.eu/project/id/820350', 'https://cordis.europa.eu/project/id/820445', 'https://cordis.europa.eu/project/id/786890', 'https://cordis.europa.eu/project/id/824295', 'https://cordis.europa.eu/project/id/769086', 'https://cordis.europa.eu/project/id/768960', 'https://cordis.europa.eu/project/id/772337', 'https://cordis.europa.eu/project/id/793965', 'https://cordis.europa.eu/project/id/797942', 'https://cordis.europa.eu/project/id/757184', 'https://cordis.europa.eu/project/id/639020', 'https://cordis.europa.eu/project/id/694665', 'https://cordis.europa.eu/project/id/679403', 'https://cordis.europa.eu/project/id/677061', 'https://cordis.europa.eu/project/id/677493', 'https://cordis.europa.eu/project/id/679843', 'https://cordis.europa.eu/project/id/677823', 'https://cordis.europa.eu/project/id/716472', 'https://cordis.europa.eu/project/id/721537', 'https://cordis.europa.eu/project/id/725627', 'https://cordis.europa.eu/project/id/694343', 'https://cordis.europa.eu/project/id/677650', 'https://cordis.europa.eu/project/id/678423', 'https://cordis.europa.eu/project/id/679243', 'https://cordis.europa.eu/project/id/846617', 'https://cordis.europa.eu/project/id/101002761', 'https://cordis.europa.eu/project/id/848325', 'https://cordis.europa.eu/project/id/850488', 'https://cordis.europa.eu/project/id/854931', 'https://cordis.europa.eu/project/id/101027737', 'https://cordis.europa.eu/project/id/101030066', 'https://cordis.europa.eu/project/id/101020016', 'https://cordis.europa.eu/project/id/101024758', 'https://cordis.europa.eu/project/id/850769', 'https://cordis.europa.eu/project/id/863227', 'https://cordis.europa.eu/project/id/861910', 'https://cordis.europa.eu/project/id/878074', 'https://cordis.europa.eu/project/id/875905', 'https://cordis.europa.eu/project/id/862111', 'https://cordis.europa.eu/project/id/846793', 'https://cordis.europa.eu/project/id/862311', 'https://cordis.europa.eu/project/id/101003472', 'https://cordis.europa.eu/project/id/101036910', 'https://cordis.europa.eu/project/id/101036449', 'https://cordis.europa.eu/project/id/101008571', 'https://cordis.europa.eu/project/id/101018420', 'https://cordis.europa.eu/project/id/101037564', 'https://cordis.europa.eu/project/id/101000622', 'https://cordis.europa.eu/project/id/101021936', 'https://cordis.europa.eu/project/id/101037424', 'https://cordis.europa.eu/project/id/101008233', 'https://cordis.europa.eu/project/id/101003470', 'https://cordis.europa.eu/project/id/101008297', 'https://cordis.europa.eu/project/id/101003815', 'https://cordis.europa.eu/project/id/956355', 'https://cordis.europa.eu/project/id/101003914', 'https://cordis.europa.eu/project/id/101017304', 'https://cordis.europa.eu/project/id/101007326', 'https://cordis.europa.eu/project/id/101003491', 'https://cordis.europa.eu/project/id/101033819', 'https://cordis.europa.eu/project/id/101016902', 'https://cordis.europa.eu/project/id/101003826', 'https://cordis.europa.eu/project/id/955317', 'https://cordis.europa.eu/project/id/101004633', 'https://cordis.europa.eu/project/id/872427', 'https://cordis.europa.eu/project/id/101006249', 'https://cordis.europa.eu/project/id/101005122', 'https://cordis.europa.eu/project/id/952851', 'https://cordis.europa.eu/project/id/855187', 'https://cordis.europa.eu/project/id/892429', 'https://cordis.europa.eu/project/id/865067', 'https://cordis.europa.eu/project/id/883520']
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
韵柒 2025-01-30 19:40:40

我希望Thi Swill解决您的问题,还请让我让我是否对您不起作用。

elems = driver.find_elements(By.XPATH,"//a[@href]")
for elem in elems:
    print(elem.get_attribute("href"))

I hope thi swill solve your question, also, please let me if this will not work for you.

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