Search code examples
pythonselenium-chromedriverhrefscreen-scraping

How to Fetch href links in Chromedriver?


I am trying to scrape the link from a button. If I click the button, it opens a new tab and I can't navigate in it. So I thought I'd scrape the link, go to it via webdriver.get(link) and do it that way since this will be a background program. I cannot find any tutorials on this using the most recent version of selenium. This is in Python

I tried using

wd.find_element("xpath", 'xpath here')

but that just scrapes the button title. Is there a different tag I should be using?

I've also tried just clicking the button but that opens a new tab and I don't know how to navigate on it, since it doesn't work by default and I'm still fairly new to Chromedriver.

I can't use beautifulsoup to my knowledge, since the webpage must be logged in.


Solution

  • You need to get the href attribute of the button. If your code gets the right button you can just use

    button.get_attribute("href")
    

    Of course if you get redirected using Javascript this is a different story, but since you didn't specify I will assume my answer works