Java 在chrome最新版本中使用selenium处理打印预览窗口
我试图在谷歌chrome 75.0.3770.80版中使用打印对话框。 我正在使用Selenium单击打印对话框上的取消按钮来关闭它 可以检查cancel按钮,它的选择器在UI上可见,但是当我尝试使用selenium单击这些选择器时,它没有给出这样的元素异常 另外,当我为该页面使用getSource()时,源代码中不存在按钮选择器,但在UI上可见Java 在chrome最新版本中使用selenium处理打印预览窗口,java,selenium,google-chrome,Java,Selenium,Google Chrome,我试图在谷歌chrome 75.0.3770.80版中使用打印对话框。 我正在使用Selenium单击打印对话框上的取消按钮来关闭它 可以检查cancel按钮,它的选择器在UI上可见,但是当我尝试使用selenium单击这些选择器时,它没有给出这样的元素异常 另外,当我为该页面使用getSource()时,源代码中不存在按钮选择器,但在UI上可见 那么,我们如何单击cancel按钮呢?有什么方法可以做到这一点吗?下面是python中的解决方案。您可以将此方法转换为java def cance
那么,我们如何单击cancel按钮呢?有什么方法可以做到这一点吗?下面是python中的解决方案。您可以将此方法转换为java
def cancelPrintPreview():
# get the current time and add 180 seconds to wait for the print preview cancel button
endTime = time.time() + 180
# switch to print preview window
driver.switch_to.window(driver.window_handles[-1])
while True:
try:
# get the cancel button
cancelButton = driver.execute_script(
"return document.querySelector('print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('print-preview-header#header').shadowRoot.querySelector('paper-button.cancel-button')")
if cancelButton:
# click on cancel
cancelButton.click()
# switch back to main window
driver.switch_to.window(driver.window_handles[0])
return True
except:
pass
time.sleep(1)
if time.time() > endTime:
driver.switch_to.window(driver.window_handles[0])
break
有关使用阴影根元素的更多信息,请查看我的答案。答案太多,太复杂了。这里有一个更好的: 这里需要做的基本上是获取触发打印弹出/对话框的默认JavaScript函数,并将其分配给空函数:
window.print = "function(){};"
以上是如何在Python中实现的。试着把它翻译成Java。Hi!请在您的问题中包含一个您已经尝试过的最小但完整的代码示例。当您只需要禁用
window.print()
JavaScript函数时,为什么要执行所有这些操作?请问是否有C版本?