Get the href value from inspect element in Python

Issue

I am searching name in LinkedIn and after that I need to extract profile URL for the first result for that I need the href value by clicking inspect element.

screen shot of inspect elementimg

url of the pagehttps://www.linkedin.com/search/results/all/?keywords=bill%20gates&origin=TYPEAHEAD_HISTORY&position=0&searchId=5b939aaa-3ded-428a-b520-7b034fa61ba1&sid=Tjr

how to extract that link in the value of href efficiently?

I have tried extracting but failed(commented those lines at the end) –

driver = webdriver.Chrome(r'chromedriver_full_path\chromedriver.exe')
driver.get("linkedin_sign_in_page_url")
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_key']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_password']")))

#enter username and password
username.clear()
username.send_keys("username")
password.clear()
password.send_keys("password")
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
#linkedin search
search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Search']")))
search.clear()
search.send_keys('bill gates')
time.sleep(4)
search.send_keys(Keys.ENTER)
time.sleep(4)
# try 1
# elems = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".app-aware-link [href]")))
# links = [elem.get_attribute('href') for elem in elems]
# links
# try 2
# print(driver.find_element_by_css_selector("p.app-aware-link > a").get_attribute('href'))

my expected output – "https://www.linkedin.com/in/williamhgates?miniProfileUrn=urn%3Ali%3Afs_miniProfile%3AACoAAA8BYqEBCGLg_vT_ca6mMEqkpp9nVffJ3hc"

Solution

Maven,
When I tried your code, I found your CSS selector in the last line of your code did not match what I can see on the same link.
So if you Replace the following line in your code:

# print(driver.find_element_by_css_selector("p.app-aware-link > a").get_attribute('href'))

with

print(driver.find_element(By.CSS_SELECTOR, ".search-results-container a.app-aware-link").get_attribute('href'))

This should get the URL you are expecting.

Answered By – ketanvj

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published