简体   繁体   English

使用 Selenium+Java 单击生成的按钮

[英]Click on generated button with Selenium+Java

This is site: https://play.alienworlds.io/这是网站: https://play.alienworlds.io/

I need to click on the login button.我需要点击登录按钮。 In HTML I can't find it...在 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>
在此处输入图像描述

The question is tricky.这个问题很棘手。 I've added the answer in Python.我已经在 Python 中添加了答案。 It uncovers the concept.它揭示了这个概念。 If you can't, I will translate the answer to Java.如果你不能,我会把答案翻译成 Java。 The approach to such tasks is really what you need.处理此类任务的方法确实是您所需要的。 I could click login button with ActionChains class.我可以使用ActionChains class 单击登录按钮。 Please not that this is not very stable case for few reasons:请注意,这不是很稳定的情况,原因如下:

1 It depends on screen resolution, so you should set the resolution after your browser is opened 1取决于屏幕分辨率,所以你应该在你的浏览器打开后设置分辨率

2 You will need to add waits (replace time.sleep() with Selenium's waits) 2您将需要添加等待(将time.sleep()替换为 Selenium 的等待)

3 You cannot move your mouse while test is executing because it will break your actions 3测试执行时您不能移动鼠标,因为它会破坏您的操作

4 And the biggest problem on my opinion is that after you click the button, you will be dealing with Captcha. 4我认为最大的问题是,点击按钮后,您将处理验证码。

This is my code which I used while debugging.这是我在调试时使用的代码。 You should set screen resolution because most probably your coordinates will be different.您应该设置屏幕分辨率,因为您的坐标很可能会有所不同。 import time进口时间

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()

I could improve it, but it is not 5 minutes task.我可以改进它,但这不是 5 分钟的任务。 Good luck!祝你好运!

ps move_by_offset moves the cursor from the upper left corner of your screen. ps move_by_offset从屏幕左上角移动 cursor。

Update:更新:

  1. I've added driver.set_window_size(1522, 754) This is the canvas resolution I get when I inspect canvas element.我添加了driver.set_window_size(1522, 754)这是我在检查 canvas 元素时得到的 canvas 分辨率。
  2. I print print(driver.get_window_size()) to make sure the screen resolution is as expected.我打印print(driver.get_window_size())以确保屏幕分辨率符合预期。
  3. I've tried few tools to measure browser window size and found out that My Ruler extension by Leann Pagac works the best for canvas.我尝试了一些工具来测量浏览器 window 大小,发现 Leann Pagac 的我的标尺扩展最适合 canvas。 All other extensions, even they are more commonly used could not measure coordinates on the canvas correctly.所有其他扩展,即使它们更常用,也无法正确测量 canvas 上的坐标。

Also, check this post to get more details on how to work with canvas How to perform Mouse Wheel scrolling over HTML5 Canvas in Selenium?另外,请查看这篇文章以获取有关如何使用 canvas 的更多详细信息如何在 HTML5 Z6EA5359E01A41842884943125518289BBB5457639DFBCA558F06 中执行鼠标滚轮滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM