Search code examples
pythonselenium-webdriverhyperlinkui-automation

driver.find_elements by tagname of "a" is showing blank results


So I got a python selenium version 4.15.2 script where I am opening this web page:

https://app.logrocket.com/

And I just want to simply fetch all the hyperlinks available in the page and store it in a collection.

But, it shows that there is no available hyperlinks in the page even though there is.

Below is the code:

driver.get("https://app.logrocket.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.TAG_NAME, 'a')))
abc = driver.find_elements(By.TAG_NAME,'a')

The variable "abc" just shows blank.

Any suggestions will be helpful for me?

Expecting all hyperlinks which generally starts from tagname as a should be stored in abc variable.


Solution

  • One of the most fundamental aspects of using Selenium is obtaining element references to work with.

    abc will be a list of references to your selected elements, to get specific information about one or multiple referenced elements you have to iterate the references and query the needed details.


    Your code should work - keep in mind that the website you provided needs a login, so far you just would be able to scrape two <a>:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
        
    driver = webdriver.Chrome()
    driver.get('https://app.logrocket.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.TAG_NAME, 'a'))) 
      
    for e in driver.find_elements(By.TAG_NAME,'a'):
        print(e.get_attribute('outerHTML'))
    

    Output:

    <a href="javascript:void(0)">Sign Up</a>
    <a class="auth0-lock-alternative-link" href="javascript:void(0)">Don't remember your password?</a>