Extract link from mouseover hover element

Viewed 160

I am trying to scrape from a mouseover hoverable element in . However, I get the error NameError: name 'campaign_link' is not defined. I am getting the feeling that I do not select the right element, but I have tried several and still can't click on the link element inside the hover element. This is my code:


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains

# Specify driver
fp = webdriver.FirefoxProfile()
fp.set_preference("network.cookie.cookieBehavior", 2)

driver = webdriver.Firefox(firefox_profile=fp)
wait = WebDriverWait(driver,10)
driver.get('https://www.indiegogo.com/projects/flash-pro-world-s-fastest-powerbank-redesigned#/')

campaign_info = driver.find_element_by_css_selector(".basicsCampaignOwner-details-name > span:nth-child(1) > span:nth-child(1) > div:nth-child(1)")
campaign_box = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".tooltipHover-hoverContent-body > div:nth-child(1)")))
campaign_link = wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR,".tooltipHover-hoverContent-body > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > a:nth-child(4)"))

actions=ActionChains(driver)
actions.move_to_element(campaign_info).move_to_element(campaign_box).move_to_element(campaign_link).click().perform()

It should go to the campaign owner webpage, but it stays on the campaign webpage. What am I doing wrong here? Could someone help me please?

1 Answers

You are using wrong, unreliable locators.
Also, you should wait for element visibility, not just to element presence.
And the main issue - the campaign_box and campaign_link elements are not clickable and even not existing until you hover over the campaign_info element.
So you have to wait for campaign_info visibility. Then hover over it and then go to campaign_link and click it!
Try this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains

# Specify driver
fp = webdriver.FirefoxProfile()
fp.set_preference("network.cookie.cookieBehavior", 2)

driver = webdriver.Firefox(firefox_profile=fp)
wait = WebDriverWait(driver,10)
driver.get('https://www.indiegogo.com/projects/flash-pro-world-s-fastest-powerbank-redesigned#/')

campaign_info = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.desktop.campaignOwnerName-tooltip.t-body--sansSerif--lg")))
actions=ActionChains(driver)
actions.move_to_element(campaign_info).perform()
campaign_link = wait.until(EC.visibility_of_element_located(By.CSS_SELECTOR,"a[class='campaignOwnerDetail-pastCampaigns t-body--sansSerif']"))
actions.move_to_element(campaign_link).click().perform()
Related