0

I am fairly new to web scraping and I started with Selenium in VBA/Excel using Chrome. My target elements are the menu items.

In some websites I can find the elements but I can't get their "href". Here is the destination website. I want the "href" of the menu items in the right side of the page.

This is what I tried:

    ' geting the menus, working well:
        Set mnus = bot1.FindElementsByClass("topmenu") 

        MenuCounter = 0
        For Each mnu In mnus
     'getting the href, fails
            lnk = mnu.Attribute("href")

I also tried some other ways but no success.

This is a screen shot of the inspect:
enter image description here

Note that I don't only want the href for this specific element (whose href is "art"). I also want the href of other equivalent menu items (except for the first item).

1
  • More information is needed. More code. More pics/information on how it is not working. Since you are new to Web Scraping, maybe you can look at Beautiful Soup and Python Commented Feb 11, 2020 at 2:55

1 Answer 1

1

The href value is inside anchor tag Not li element.You need to target anchor tag.Use following css selector.

Set mnus = bot1.FindElementsByCssSelector("li.topmenu >a[href]")    
MenuCounter = 0
For Each mnu In mnus
    lnk = mnu.Attribute("href")

Or To get all menus link try this.

To get all menus link

Set mnus = bot1.FindElementsByCssSelector("ul.topmenu a[href]")
MenuCounter = 0
For Each mnu In mnus
    lnk = mnu.Attribute("href")
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. that was exaclty what I needed. And after reading your response i have been searching for hours to realize the syntax. I found good material in w3schools.com/cssref/css_selectors.asp so what I learned is this syntax: set x= bot.FindelementsbyCss(" element.class > childrenelement [ desired attribute]") . Am I getting it right?
Possibly yes.I don’t have much knowledge about VBA.But the css selector is correct just check the syntax.
I also found out that i can use this selector for the Menu items: #css3menu1 > li > a . Tha gives me all Menu items (including the first one) I guess I have learnt the basics of the concept. Many thanks Kunduk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.