Selenium WebDriverException:期待' id'鼠标要映射到子类型未定义的InputState,得到:pointerMove

时间:2017-03-22 11:09:53

标签: selenium selenium-webdriver selenium-firefoxdriver

我对Selenium有一个问题,我无法理解。此外,我无法通过Google找到有关此问题的大量信息。

我的Selenium脚本执行以下步骤:

  1. 登录Facebook。
  2. 转到朋友提案列表。
  3. 向下滚动几次(为了加载更多提案)。
  4. 在控制台上逐一展示所有提案,并询问用户是否应添加该朋友。
  5. 确认后,会创建一个动作链,移动到相关提案,然后点击添加按钮。

    但是Action链不起作用。我收到以下错误:

    Potential friend name: 'John Doe'
    Social context: 'Max Mustermann und 3 weitere gemeinsame Freunde'
    Traceback (most recent call last):
      File "c:\...\facebook_selenium_minimal.py", line 74, in <module>
        main()
      File "c:\...\facebook_selenium_minimal.py", line 57, in main
        friend_add_button).perform()
      File "C:\Python36\lib\site-packages\selenium\webdriver\common\action_chains.py", line 77, in perform
        self.w3c_actions.perform()
      File "C:\Python36\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 76, in perform
        self.driver.execute(Command.W3C_ACTIONS, enc)
      File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
        self.error_handler.check_response(response)
      File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: Message: Expected 'id' mouse to be mapped to InputState whose subtype is undefined, got: pointerMove
    

    这是我的Selenium脚本:

    import time
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.ui import WebDriverWait  # available since 2.4.0
    from selenium.webdriver.support import expected_conditions as EC  # available since 2.26.0
    from selenium.webdriver.common.action_chains import ActionChains
    
    TIMEOUT = 5
    
    def main():
        driver = webdriver.Firefox()
        driver.get("http://www.facebook.com")
    
        print(driver.title)
    
        input_mail = driver.find_element_by_id("email")
        input_password = driver.find_element_by_id("pass")
    
        input_mail.send_keys("your_login@example.com")
        input_password.send_keys("your_password")
        input_password.submit()
    
        try:
            WebDriverWait(driver, TIMEOUT).until(
                EC.visibility_of_element_located((By.NAME, "requests")))
    
            driver.get("https://www.facebook.com/friends/requests/?fcref=jwl")
    
            WebDriverWait(driver, TIMEOUT).until(
                EC.visibility_of_element_located((By.ID, "fbSearchResultsBox")))
    
            # Let Facebook load more friend proposals.
            for i in range(2):
                driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
                time.sleep(1.0)
    
            friend_proposals = driver.find_elements_by_class_name(
                "friendBrowserListUnit")
    
            for friend_proposal in friend_proposals:
                try:
                    friend_title = friend_proposal.find_element_by_class_name(
                        "friendBrowserNameTitle")
                except NoSuchElementException:
                    print("Title element could not be found. Skipping.")
                    continue
    
                print("Potential friend name: '%s'" % friend_title.text)
    
                social_context = friend_proposal.find_element_by_class_name(
                    "friendBrowserSocialContext")
                social_context_text = social_context.text
                print("Social context: '%s'" % social_context_text)
    
                friend_add_button = friend_proposal.find_element_by_class_name(
                    "FriendRequestAdd")
    
                actions = ActionChains(driver)
                actions.move_to_element(friend_proposal).move_to_element(
                    friend_add_button).perform()
                time.sleep(0.1)
    
                print("Should I add the friend (y/N): ")
                response = input()
                if response == "y":
                    friend_add_button.click()
                    time.sleep(1.0)
                    print("Added friend...")
    
        except TimeoutException as exc:
            print("TimeoutException: " + str(exc))
        finally:
            driver.quit()
    
    if __name__ == '__main__':
        try:
            main()
        except:
            raise
    

    我使用的是最新的Selenium版本:

    C:\Users\Robert>pip show selenium
    Name: selenium
    Version: 3.3.1
    

    我有使用geckodriver v0.15.0的Firefox 52.0.1。

    更新:快速测试显示,相同的脚本可以与Chrome Webdriver完美配合。

    更新2: Github上的Selenium bugtracker中的这个问题可能有关:https://github.com/SeleniumHQ/selenium/issues/3642

2 个答案:

答案 0 :(得分:2)

我今天遇到了同样的问题。您可能已经观察到第一个move_to_element和perform()有效 - 至少在我的情况下这是真的。要重复此操作,您应该重置for循环中的操作链:

actions.perform()

actions.reset_actions()

答案 1 :(得分:0)

对我来说 - .perform第一次失败 - 我在selenium 3.3.1,gecko 15和最新的firefox使用java - 相同的代码在chrome上完美运行。