Python Selenium 操作链可以工作,但会在 Firefox 中停止我的程序

问题描述 投票:0回答:2

我有时使用 ActionsChains 时遇到任何问题,今天他工作但停止了我的程序,你知道为什么吗?

    scrolling_bar = driver.find_element(By.CSS_SELECTOR, "#scrolling_bar")
    start = scrolling_bar.location
    ActionChains(driver)\
        .drag_and_drop_by_offset(scrolling_bar, start['x'], start['y'] - 1000)\
        .perform()
    print('This message never be said')
    ActionChains(driver).reset_actions()

动作已执行但停止我的程序

python selenium-webdriver firefox
2个回答
0
投票

使用显式等待来确保元素在与其交互之前可见。 尝试

click_and_hold
move_by_offset
,然后释放而不是
drag_and_drop_by_offset

driver = webdriver.Firefox()

driver.get('YOUR_URL_HERE')

scrolling_bar = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.CSS_SELECTOR, "#scrolling_bar"))
)

actions = ActionChains(driver)
actions.click_and_hold(scrolling_bar).move_by_offset(0, -1000).release().perform()

print('Drag and drop performed')

driver.quit()

0
投票

我解决了问题,-1000 太贵了...谢谢你的回答!!

© www.soinside.com 2019 - 2024. All rights reserved.