使用selenium+java单击生成的按钮

wlsrxk51  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(412)

这是网站: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>

igsr9ssn

igsr9ssn1#

这个问题很棘手。我已经用python添加了答案。它揭示了这个概念。如果你不能,我会把答案翻译成java。处理这些任务的方法才是你真正需要的。我可以点击登录按钮 ActionChains 班级。请注意,这不是一个非常稳定的情况,原因如下:
1它取决于屏幕分辨率,因此您应该在打开浏览器后设置分辨率
2您需要添加等待(替换 time.sleep() 与 selenium 的等待)
3您不能在测试执行时移动鼠标,因为它会打断您的操作
在我看来,最大的问题是,在你点击按钮之后,你将处理captcha。
这是我调试时使用的代码。你应该设置屏幕分辨率,因为你的坐标很可能是不同的。导入时间

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分钟的任务。祝你好运!
附笔 move_by_offset 从屏幕左上角移动光标。
更新:
我加了一句 driver.set_window_size(1522, 754) 这是我检查canvas元素时得到的画布分辨率。
我打印 print(driver.get_window_size()) 以确保屏幕分辨率符合预期。
我尝试了一些工具来测量浏览器窗口的大小,发现leann pagac的标尺扩展最适合canvas。所有其他扩展,即使是更常用的扩展,也无法正确测量画布上的坐标。
另外,查看这篇文章可以获得更多关于如何使用canvas的细节,如何在selenium中在html5canvas上执行鼠标滚轮滚动?

相关问题