jd_shop_info4.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. class SlideUtils:
  2. @staticmethod
  3. def slide_by_action_chains(driver, degree):
  4. # 通过 JavaScript 执行鼠标事件
  5. script = """
  6. degree=arguments[0]
  7. function generateSmartTrajectory(targetX) {
  8. const trajectory = [];
  9. let x = 0;
  10. let t = 0;
  11. trajectory.push([x, 0, t]);
  12. while (x < targetX * 0.3) {
  13. const dx = Math.random() < 0.7 ? 1 : 2;
  14. x += dx;
  15. t = 12 + Math.floor(Math.random() * 10); // 12~22ms
  16. trajectory.push([x, 0, t]);
  17. }
  18. while (x < targetX - 8) {
  19. const dx = 2 + Math.floor(Math.random() * 3); // 起步步长2~4
  20. x += dx;
  21. t = 10 + Math.floor(Math.random() * 8); // 停留10~18ms
  22. trajectory.push([x, 0, t]);
  23. }
  24. const overshoot = targetX + Math.floor(Math.random() * 7) + 3; // 2~4 px 超过
  25. while (x < overshoot) {
  26. const dx = Math.random() < 0.8 ? 1 : 0; // 单次步长1为主
  27. x += dx;
  28. t = 28 + Math.floor(Math.random() * 25); // 停留28~53ms
  29. trajectory.push([x, 0, t]);
  30. }
  31. while (x > targetX) {
  32. const dx = Math.random() < 0.6 ? -1 : 0; // 回退
  33. x += dx;
  34. t = 50 + Math.floor(Math.random() * 40); // 停留50~90ms
  35. trajectory.push([x, 0, t]);
  36. }
  37. const lingerCount = 2 + Math.floor(Math.random() * 3); // 结束 2~4 次
  38. for (let i = 0; i < lingerCount; i++) {
  39. t = 100 + Math.floor(Math.random() * 250); // 停留100~350ms
  40. trajectory.push([targetX, 0, t]);
  41. }
  42. return trajectory;
  43. }
  44. async function jdRotate_slide(trajectory) {
  45. const element = document.querySelector('#slider-div');
  46. coordinates = element.getBoundingClientRect();
  47. console.log(coordinates)
  48. centerX = coordinates.x + 10
  49. centerY = coordinates.y + 10
  50. const mouseEvent = (type, x, y) => {
  51. const event = new MouseEvent(type,{
  52. bubbles: true,
  53. cancelable: true,
  54. clientX: x,
  55. clientY: y,
  56. });
  57. element.dispatchEvent(event);
  58. }
  59. ;
  60. mouseEvent('mousedown', centerX, centerY);
  61. let y = 0
  62. for (let i = 1; i < trajectory.length; i++) {
  63. [x,k,t] = trajectory[i];
  64. y = y + Math.floor(Math.random() * 2);
  65. mouseEvent('mousemove', centerX + x, centerY + y);
  66. await new Promise(resolve => setTimeout(resolve, t));
  67. }
  68. mouseEvent('mouseup', centerX + x, centerY + y);
  69. }
  70. x =degree/1.5
  71. track_ = generateSmartTrajectory(x)
  72. jdRotate_slide(track_)
  73. """
  74. driver.execute_script(script, degree)