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?
CodePudding user response:
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)
CodePudding user response:
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 a element i.e. 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"))