简体   繁体   中英

Python Selenium - Get href link

I'm trying to get the Facebook link from a website href element, but keep getting errors. Here is the HTML from its website:

   <li class="ssi-facebook">
      <a href="http://www.facebook.com/pages/YMCA-of-Central-Massachusetts/165183010213237?sk=wall" target="_blank" rel="noopener noreferrer">
      </a>
    </li>

and here is the code I've tried:

elems = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located(
        (By.CLASS_NAME, 'ssi-facebook')))
links = [elem.get_attribute('href') for elem in elems]
print(links)

Do you have any idea how to solve this?

li does not have href , it's a tag that has href .

You can use the below CSS selector instead

elems = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'li.ssi-facebook a')))
links = [elem.get_attribute('href') for elem in elems]
print(links)

There are a couple of things to consider:

  • The href link is within the child A of the parent LI

  • As you are dealing with only one element instead of element(s) you can look for the unique element.

  • To print the href attribute, as the desired element is aelement ie a clickable element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li.ssi-facebook > a[href]"))).get_attribute("href"))
  • Using XPATH :

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//li[@class='ssi-facebook']/a[@href]"))).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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM