簡體   English   中英

有沒有辦法使用 Selenium 在屏幕外點擊坐標?

[英]Is there a way to click at coordinates off-screen using Selenium?

我正在嘗試使用 Selenium 在屏幕外單擊坐標。 我發現的所有內容都說滾動到元素/坐標。 但是,我無法滾動。 該頁面適合單個屏幕。

我嘗試了以下方法,但它給了我一個錯誤。

代碼:

Actions a = new Actions(driver);
a.moveByOffset(10000, 10000).click().perform();

錯誤:

Exception in thread "Thread-0" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: move target out of bounds: viewport size: 1680, 896
  (Session info: MicrosoftEdge=100.0.1185.29)
Build info: version: '4.0.0', revision: '3a21814679'
System info: host: 'DESKTOP-28HQ274', ip: '10.0.0.2', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '16.0.2'
Driver info: org.openqa.selenium.edge.EdgeDriver
Command: [1732248391f4756fc95c00a6d0f737c0, actions {actions=[org.openqa.selenium.interactions.Sequence@61904d24]}]
Capabilities {acceptInsecureCerts: false, browserName: msedge, browserVersion: 100.0.1185.29, javascriptEnabled: true, ms:edgeOptions: {debuggerAddress: localhost:54306}, msedge: {msedgedriverVersion: 100.0.1185.29 (1feddedadb21..., userDataDir: C:\Users\User\AppData\Local...}, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:54306/devtoo..., se:cdpVersion: 100.0.1185.29, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 1732248391f4756fc95c00a6d0f737c0
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:139)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:559)
    at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:627)
    at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:617)
    at org.openqa.selenium.interactions.Actions.perform(Actions.java:573)
    at kingrabb.it.seleniumtest.Main.lambda$new$0(Main.java:40)
    at java.base/java.lang.Thread.run(Thread.java:831)

我的 Main.java 中的第 40 行(如錯誤中所述)是:

a.moveByOffset(10000, 10000).click().perform();

任何幫助是極大的贊賞

如評論中所述,您無法單擊視口。

但是,您可以修改瀏覽器大小以占據整個屏幕:

driver.manage().window().setSize(new Dimension(1680, 896));

window 也可以通過以下方式最大化:

driver.manage().window().maximize();

這擴展了您可以單擊的空間,並且(據我所知)由於 Selenium 當前的功能,它是最佳解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM