Home > other >  How do I scrape the text that appears when I hover over the element?
How do I scrape the text that appears when I hover over the element?

Time:03-03

So on the e-commerce webpage (enter image description here

CodePudding user response:

for some reason I couldn't get your site to work with the platform I am using but I managed to create an example. I would recommend using the Actions library and use it to move to the element in question.

ActionChains(self.driver).move_to_element(self.driver.find_element(By.XPATH, "//a[@class='dropdown-toggle']")).perform()

This is for the site I am using but if you change the xpath for your own element it should work. Once the driver has done this you can find the element you want that is revealed by hovering. You should be able to just put this in a for loop and you are good to go.

I made a runnable example here

CodePudding user response:

Here is the solution I came to before I saw the answers here:

 elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='Swatch_swatch__2X1CY']")))
    for el in elements:
        ActionChains(driver).move_to_element(el).perform()
        page = BeautifulSoup(driver.page_source, features='html.parser')
        print(page.find("div", class_="ui top left popup transition visible Tooltip_Tooltip__M0LJL Tooltip_black__heZoQ").text)
  • Related