【问题标题】:how to simulate the hover of cursor python selenium如何模拟光标python selenium的悬停
【发布时间】:2020-06-30 16:45:54
【问题描述】:

我想点击网站上的按钮,但是当你把光标放在正确的地方后这个按钮出现了,否则你看不到这个按钮。我试图忽略这一点,并通过 xpath 查找元素来进行正常单击。但这并没有成功,出现了错误:

selenium.common.exceptions.ElementNotInteractableException: Message: element not 可交互的:元素大小为零

如何处理?

【问题讨论】:

    标签: python selenium hover


    【解决方案1】:

    要将鼠标悬停在元素上,您可以使用以下内容:

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    
    chrome= webdriver.Chrome()
    chrome.get('http://foo.bar')
    element = chrome.find_element_by_css_selector("the_element_u_want")
    
    hover = ActionChains(chrome).move_to_element(element)
    hover.perform()
    

    如果元素需要时间出现,请使用 selenium 的 waittime.sleep()

    了解更多关于 ActionChains 的信息here

    【讨论】:

    • 我还是有问题。为了清楚起见:这是我正在谈论的一个网站 - vk.com/durov?z=photo1_456311217%2Fphotos1,我想单击将图像切换到右侧的按钮(看起来像“>”)
    • 但由于某种原因我的代码无法正常工作,您能帮帮我吗?
    【解决方案2】:

    这是我找到的一些代码here

    通常,元素未完全加载时会抛出 ElementNotInteractableException。这是一个可能的解决方案:

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as ec
    
    driver = webdriver.Chrome('C:\\Users\\dell\\Libs\\chromedriver.exe')
    
    driver.get("http://url")
    driver.maximize_window()
    
    # wait for element to appear, then hover it
    wait = WebDriverWait(driver, 10)
    men_menu = wait.until(ec.visibility_of_element_located((By.XPATH, "//a[@data-tracking-id='men']")))
    ActionChains(driver).move_to_element(men_menu).perform()
    

    【讨论】:

      【解决方案3】:

      这个方法对我很有效。

      def hoverXPATH(xpath):
         item = WebDriverWait(driver, 10)\
         .until(EC.visibility_of_element_located((By.XPATH, xpath)))
         ActionChains(driver).move_to_element(item).perform()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        • 2012-12-15
        相关资源
        最近更新 更多