Home > other >  How can I get the a tag inside a li using selenium
How can I get the a tag inside a li using selenium

Time:09-24

im coding a program to pull data from sites to ease my job at my company I need to get the contents of all "a" tags inside "li" tags ive tried it by finding all elements by their xpath but it yielded no result here's my code and output:

def getLinks(link):
    driver.get(link)
    for i in range(0,10):
        result = driver.find_elements_by_xpath(f"/html/body/div[3]/div/div[1]/div[2]/div[3]/ul/li[{i}]/a")
        for r in result:
            print(r.text)
    # for result in results:
    # print(r)

    driver.close()

trying to get the path of all a tags inside li tags here

output should look like:

http://turkosb.com/presmetal-otomotiv.html
    for all li elements

but what I get is:

   Presmetal Otomotiv Yan San. A.Ş.
    Belirtilmemiş
     90 224 484 30 30



ps: driver.find_elements_by_xpath(f"/html/body/div[3]/div/div[1]/div[2]/div[3]/ul/li[{i}]/a") this code refers to variable xpath's of all a elements inside li elements

CodePudding user response:

You want href attribute but trying r.text which return the Text of the Element.

Use get_attribute() to get the href of all a tags.

And try Relative xpath like below. (As per the screen shot)

//ul[@class='box-listing']/li/a
options = driver.find_elements_by_xpath("//ul[@class='box-listing']/li/a") # Xpath to find all the `a` tags.
for option in options:
    print(option.get_attribute("href"))
  • Related