是否可以在Selenium中加快move_to_element()或其他替代方法?

抓取网页时触发onmouseover事件的最快方法是什么?

所以我想将鼠标移到div元素上,然后调用javascript函数来更新另一个div(显示我要抓取的工具提示)。当将鼠标移离第一个div时,工具提示会消失,但仍可以进行抓取,因为只有显示样式设置为无。 我想使此过程尽快完成。

当前,我使用Selenium和move_to_element大约需要0.55s。这很长,因为我最终必须重复此过程> 60次。

start_time = time.time()
action = actionChains(driver)
action.move_to_element(box)
action.perform()
print("Time: ",time.time() - start_time()) # ~0.5s

当我在scrapy shell中使用它时,我看到此硒请求显示以下消息。

[selenium.webdriver.remote.remote_connection] DEBUG: POST http://127.0.0.1:57309/session/49aea696ad5c6a23dd1a292adacf1d92/actions {"actions": [{"parameters": {"pointerType": "mouse"},"type": "pointer","id": "mouse","actions": [{"duration": 250,"x": 0,"type": "pointerMove","y": 0,"origin": {"element-6066-11e4-a52e-4f735466cecf": "63f7afad-d9d6-49c2-989a-75c6d083c055"}}]},{"type": "key","id": "key","actions": [{"duration": 0,"type": "pause"}]}]}

在“动作”中显示为“持续时间:250”,我怀疑这是move_to_element动作的持续时间。我找不到任何方法来手动减少此持续时间,这可能吗? 我还听说scrapy-splash比硒快得多,但尚未找到很多使用方法。它也可以模拟悬停事件,还是可以使用正确的参数直接调用javascript函数?

DLL33DLL33 回答:是否可以在Selenium中加快move_to_element()或其他替代方法?

有两件事:

  • onmouseoveronmouseover事件在将鼠标指针移到图像上时执行JavaScript。一个例子:

    <img onmouseover="bigImg(this)" src="smiley.gif" alt="Smiley">
    
  • move_to_element(to_element)move_to_element(to_element)将鼠标移动到元素的中间。

  • 您测量的时间(即〜0.55s )还涉及其他一些活动以及move_to_element(to_element),例如:
    • action = ActionChains(driver)
    • action.perform()
    • print()
  • 在这一点上,值得一提的是,[action_chains](https://selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html#module-selenium.webdriver.common.action_chains)的方法默认情况下可确保所需元素为is_displayed()中的is_enabled()DOM Tree

结论

考虑到上述事实,~500-550 ms的时间跨度看起来非常优化。

本文链接:https://www.f2er.com/3054209.html

大家都在问