I am new in Selenium and I am doing web scraping of a site, in that I want to get all href links of a tag.
I used the below code but unable to get the href link. It displays javascript:
as output.
driver.find_element_by_css_selector('div.clFx>a').get_attribute('href')
In other code, this works fine but here it shows nothing, I am also attaching the image of the inspect element area where I want to fetch the href link.
I have also checked some of the answers in Stack Overflow and used the same code still I am unable to get it.
<div class="clFx">
::before
<a class="userName name" href="https://resdex.naukri.com/v2/preview/preview?uniqId=6f44e0e0b95503a44378054b64bdb1cc580e0f001e115d110418475f5808004f130d020214495f5e0b544e170d6&sid=3922138883&paramString=2faf4d57a73f0d419d15309cbc9f5f67134f5108084a5746754e034a571b2513445055524d51250c4b0a1f57504f54030c6&hfFlowName=search&commentSearchType=comment-my,comment-others" target="_blank">Bhimanagoud Patil</a>
::after
</div>
The above href link I want to get it.
I have included below the image of the inspect element:
You can directly use anchor tag to retrieve href attribute as its associated with it. Its declare inside the web element interface and it returns the value of the web element attribute as a string
wait = WebDriverWait(driver, 20)
element= wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Bhimanagoud Patil"))).get_attribute("href")
print element
or
wait = WebDriverWait(driver, 20)
element= wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='userName name']"))).get_attribute("href")
print element
Note: please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
You can also use xpath in this way:
driver.find_element_by_xpath('//div[@class="clFx"]/a').get_attribute('href')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.