单击带有 Selenium+Java 的生成按钮

Click on generated button with Selenium+Java

这是网站: https://play.alienworlds.io/

我需要点击登录按钮。 在 HTML 我找不到它...

<body>
   <div class="webgl-content">
      <div id="unityContainer" style="width: 1528px; height: 355px; padding: 0px; margin: 0px; border: 0px; position: relative; background: url(&quot;Build/fff2a664dc06d7254246c6bb8d5d0e21.jpg&quot;) center center / cover;">
         <canvas id="#canvas" style="width: 100%; height: 100%; cursor: default;" width="1528" height="355"></canvas>
         <div class="logo " style="display: none;"></div>
         <div class="progress Dark" style="display: none;">
            <div class="empty" style="width: 0%;"></div>
            <div class="full" style="width: 100%;"></div>
         </div>
      </div>
   </div>
   <script src="Build/061f463856577255fb5eefaf73e67127.js" type="text/javascript"></script>
   <script src="bundle.js" type="text/javascript"></script>
   <script src="hashes.js" type="text/javascript"></script>
   <script src="message_handler.js" type="text/javascript"></script>
   <script type="text/javascript">
      // Initial screen setup
      unityContainer.style.height = window.innerHeight + "px";
      unityInstance = UnityLoader.instantiate(
        "unityContainer",
        "Build/bd2a2f07495f744fece3ee93c4a56908.json",
        { onProgress: UnityProgress }
      );
      
      function Resize() {
        document.getElementById('unityContainer').style.width =
            window.innerWidth + 'px';
          document.getElementById('unityContainer').style.height =
            window.innerHeight + 'px';
      
      
         if (UnityLoader.SystemInfo.mobile) {
          if (navigator.userAgent.indexOf('Android') != -1)
          {
            if (screen.width > screen.height) {
              unityInstance.SendMessage("Canvas", "SetScreenModePanel", "false");
            }
            else
            {
              unityInstance.SendMessage("Canvas", "SetScreenModePanel", "true");
            }
          }
          else
          {
            switch (window.orientation) {
            case -90:
            case 90:
              unityInstance.SendMessage('Canvas', 'SetScreenModePanel', 'false');
              break;
            default:
              unityInstance.SendMessage('Canvas', 'SetScreenModePanel', 'true');
      
              break;
            }
          }
        } 
      }
      
      /*function Fullscreen() {
        unityInstance.SetFullscreen(1);
        if (navigator.userAgent.indexOf('Android') != -1)
          window.screen.orientation.lock('landscape');
      }*/
      
      window.addEventListener('orientationchange', Resize);
      
      // Initial execution if needed
      //Resize();
   </script>
   <script src="blob:https://play.alienworlds.io/9ffeac95-ddb2-480b-a75f-a20043229a5b" id="0941210686ad38c201cd5aecd353ebd4"></script>
</body>

这个问题很棘手。 我已经在 Python 中添加了答案。它揭示了这个概念。如果你不会,我会把答案翻译成Java。执行此类任务的方法正是您所需要的。 我可以用 ActionChains class 单击登录按钮。 请注意,由于以下几个原因,这不是很稳定的情况:

1 视屏幕分辨率而定,请在打开浏览器后设置分辨率

2 您需要添加等待(将 time.sleep() 替换为 Selenium 的等待)

3 测试执行时您不能移动鼠标,因为它会破坏您的操作

4 我认为最大的问题是点击按钮后,您将与验证码打交道。

这是我在调试时使用的代码。您应该设置屏幕分辨率,因为您的坐标很可能会有所不同。 导入时间

from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
url = "https://play.alienworlds.io/"
driver.get(url)
driver.set_window_size(1522, 754)  # setting window size to be sure what resolution you work with
time.sleep(20)
driver.implicitly_wait(25)
canvas = driver.find_element_by_css_selector("canvas[id='#canvas']")
print(driver.get_window_size()) # print to make sure window size is correct
drawing = ActionChains(driver)
print("moving by offset")
drawing.move_by_offset(450, 511)
print("clicking")
drawing.click()
print("clicked")
drawing.perform()
print("performed")
time.sleep(5)  # just to check that a new window is opened
driver.close()
driver.quit()

我可以改进它,但这不是 5 分钟的任务。 祝你好运!

p.s。 move_by_offset 将光标从屏幕的左上角移开。

更新:

  1. 我添加了 driver.set_window_size(1522, 754) 这是我在检查 canvas 元素时得到的 canvas 分辨率。
  2. 我打印 print(driver.get_window_size()) 以确保屏幕分辨率符合预期。
  3. 我尝试了一些工具来测量浏览器 window 大小,发现 Leann Pagac 的 My Ruler 扩展最适合 canvas。所有其他扩展,即使它们更常用,也无法正确测量 canvas 上的坐标。

此外,请检查此 post 以获取有关如何使用 canvas How to perform Mouse Wheel scrolling over HTML5 Canvas in Selenium?

的更多详细信息