Home > Back-end >  selenium - for loop accumulating previous results
selenium - for loop accumulating previous results

Time:08-12

Code works but the results are accumulating the previous one, please help me thankyou.

url=https://www.bbc.com/news/world

news_search = driver.find_elements(By.XPATH, "//div[@class='gs-c-promo gs-t-News nw-c-promo gs-o-faux-block-link gs-u-pb gs-u-pb @m nw-p-default gs-c-promo--inline gs-c-promo--stacked@xl gs-c-promo--flex']")
title = []
link = []

for search in news_search:
    title.append(search.find_element(By.XPATH, "//h3[@class='gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text']").text)
    link.append(search.find_element(By.XPATH, ".//a[@class='gs-c-promo-heading gs-o-faux-block-link__overlay-link gel-pica-bold nw-o-link-split__anchor']").get_attribute('href'))
    print(f'Title:{title}\nLink:{link}')
driver.quit()

enter image description here

CodePudding user response:

You were printing the list of all titles and links, rather than the current one. Change it to the following:

news_search = driver.find_elements(By.XPATH, "//div[@class='gs-c-promo gs-t-News nw-c-promo gs-o-faux-block-link gs-u-pb gs-u-pb @m nw-p-default gs-c-promo--inline gs-c-promo--stacked@xl gs-c-promo--flex']")
titles = []
links = []

for search in news_search:
    title = search.find_element(By.XPATH, ".//h3[@class='gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text']").text
    link = search.find_element(By.XPATH, ".//a[@class='gs-c-promo-heading gs-o-faux-block-link__overlay-link gel-pica-bold nw-o-link-split__anchor']").get_attribute('href')
    print(f'Title:{title}\nLink:{link}')
    titles.append(title)
    links.append(link)
driver.quit()

CodePudding user response:

Your code is almost there, you just need to add a dot . for the title element XPath expression as following:

news_search = driver.find_elements(By.XPATH, "//div[@class='gs-c-promo gs-t-News nw-c-promo gs-o-faux-block-link gs-u-pb gs-u-pb @m nw-p-default gs-c-promo--inline gs-c-promo--stacked@xl gs-c-promo--flex']")
title = []
link = []

for search in news_search:
    title.append(search.find_element(By.XPATH, ".//h3[@class='gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text']").text)
    link.append(search.find_element(By.XPATH, ".//a[@class='gs-c-promo-heading gs-o-faux-block-link__overlay-link gel-pica-bold nw-o-link-split__anchor']").get_attribute('href'))
    print(f'Title:{title}\nLink:{link}')
driver.quit()

//h3[@class='gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text'] XPath will return the first match for this locator on the entire DOM while if you use this expression with leading dot . it will locate the element inside the search web element .//h3[@class='gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text']

CodePudding user response:

Just try to add a "." in your title.append function like this:

    title.append(search.find_element(By.XPATH, ".//h3[@class='gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text']").text)
  • Related