Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

Unable to hover over element using Selenium Python

I have an element defined like below:

def InspectElement():
    global element
    element = driver.switch_to.active_element
    print(element)
    return element

I am trying to hover over it like so:

def MoveMouse():
    print('MovingMouse')
    ActionChains(driver).move_to_element(element).perform()
    return

This doesn't work. I think I need to get the xpath of the element instead of just asking it to click the element itself? Could that be it? If so, is there a way to get the xpath of the element? Googling it only comes up with ways to search by xpath, but I want to return xpath as an attribute of the element. Is that possible? Or am I going about it the wrong way?

question from:https://stackoverflow.com/questions/65545635/unable-to-hover-over-element-using-selenium-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can find the xpath of an element by going to Chrome -> Right click Element -> Inspect -> Right click HTML -> Copy -> Copy XPATH

I would solve it like this:

def inspectElementMoveMouse():
    element = driver.find_element_by_xpath('/html/body/header/div/div[1]/a[2]/span')
    print('MovingMouse')
    ActionChains(driver).move_to_element(element)
    element.perform()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...