Selenium for python KeyError:无

时间:2017-11-03 13:52:50

标签: python selenium

   I want to do move button at some description。

然后是我的代码:

 action_chains = ActionChains(self.driver)
    while move_x_sum < offset_x - 10:
        move_x = random.randint(5, 10)
        action_chains.move_to_element_with_offset(
            to_element=move_button,
            xoffset=move_x,
            yoffset=offset_y).click_and_hold(move_button).pause(float(random.randint(300, 500) / 1000))
        # time.sleep(float(random.randint(300, 500) / 1000))
        # action_chains.pause(random.randint(300, 500) / 1000)
        move_x_sum += move_x
    action_chains.move_to_element_with_offset(
        to_element=move_button,
        xoffset=offset_x - move_x_sum,
        yoffset=offset_y)
    action_chains.perform()

当运行到action_chains.perform()时。它会抛出异常。

在线:

action_chains.perform()

错误堆栈跟踪:

  File "/Library/Python/2.7/site-packages/selenium/webdriver/common/action_chains.py", line 83, in perform
    action()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/common/action_chains.py", line 323, in <lambda>
    time.sleep(seconds)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 306, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 459, in execute
    command_info = self._commands[command]
KeyError: None

我该怎么办?

1 个答案:

答案 0 :(得分:0)

尝试稍微打破链条。我认为你的链中嵌套太多了。 我遇到了action_chains.pause()命令的问题 - 不支持。所以使用time.sleep()就像你已经注释掉了一样。

from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import math
import random
import time

options = Options()
options.add_argument("--disable-infobars")
driver = webdriver.Chrome(chrome_options=options)

driver.get('http://whatever_your_url_is')
move_button = driver.find_element_by_id('the_button')

move_x_sum = 0
offset_x = 20
offset_y = 30
action_chains = ActionChains(driver)
while move_x_sum < offset_x - 10:
    move_x = random.randint(5, 10)
    action_chains.move_to_element_with_offset(
        to_element=move_button,
        xoffset=move_x,
        yoffset=offset_y)
    action_chains.click_and_hold(move_button)
    action_chains.perform()
    time.sleep(float(random.randint(300, 500) / 1000))
    move_x_sum += move_x
action_chains.move_to_element_with_offset(
    to_element=move_button,
    xoffset=offset_x - move_x_sum,
    yoffset=offset_y)
action_chains.perform()
time.sleep(2)
driver.quit()