繁体   English   中英

如何通过使用Java在Selenium Webdriver中的弹出窗口上单击允许按钮

[英]How to click on allow button on popup window in selenium Webdriver by using Java

我正在使用自动化安装Firefox扩展。 我正在使用下面的代码。 但我遇到了错误。

在此处输入图片说明

 package com.toolbar.pages; import java.io.Console; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.sikuli.script.FindFailed; import org.sikuli.script.ImagePath; import org.sikuli.script.Pattern; import org.sikuli.script.Screen; public class FFInstallationpage { @SuppressWarnings("deprecation") public static void main(String[] args) throws FindFailed, InterruptedException { WebDriver driver; DesiredCapabilities capabilities = new DesiredCapabilities(); System.setProperty("webdriver.gecko.driver", "/Users/venkata.b/Downloads/geckodriver"); driver =new FirefoxDriver(capabilities); driver.manage().window().maximize(); driver.get("https://toolbar.rakuten.co.jp/ff/"); driver.findElement(By.xpath("//img[contains(@src,'https://image.infoseek.rakuten.co.jp/content/toolbar/ff/top/main_btn.png')]")).click(); Thread.sleep(5000); driver.findElement(By.xpath("//img[contains(@src,'https://image.infoseek.rakuten.co.jp/content/toolbar/install_btn.gif')]")).click(); Alert alert = driver.switchTo().alert(); driver.switchTo().alert(); alert.accept(); 

错误

线程“主”中的异常org.openqa.selenium.NoAlertPresentException:当前未打开任何模式对话框生成信息:版本:'3.7.1',修订版:'8a0099a',时间:'2017-11-06T21:07:31.527Z '系统信息:主机:'RINMAC297',ip:'fe80:0:0:0:c44:2da2:148c:dc85%en0',操作系统名称:'Mac OS X',os.arch:'x86_64', os.version:'10 .12.6',java.version:'1.8.0_144'驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver功能{acceptInsecureCerts:false,browserName:firefox,browserVersion:57.0,javascriptEnabled:true,moz :accessibilityChecks:false,moz:headless:false,moz:processID:8761,moz:profile:/ var / folders / b0 / rz6ystbx6q7 ...,moz:webdriverClick:false,pageLoadStrategy:正常,平台:MAC,平台名称:MAC ,platformVersion:16.7.0,可旋转:false,超时:{隐式:0,pageLoad:300000,脚本:30000}}会话ID:3cb929c6-a392-b247-a6ec-72af24f1ab6f在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法),位于sun.reflect.NativeConstructorAccessorImpl.newInsta 在org.openqa.selenium.remote.http.sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)在java.lang.reflect.Constructor.newInstance(Constructor.java:423)的nce(NativeConstructorAccessorImpl.java:62) .W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)位于org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) :49)在org.openqa.selenium的org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)的org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)处。在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:657)处的remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)在org.openqa.selenium.remote.RemoteWebDriver $ RemoteTargetLocator.alert(java.org 986),位于com.toolbar.pages.FFInstallationpage.main(FFInst allationpage.java:50)

如果您希望收到警报,但有时它可能无法打开。您可以使用try and catch处理它

try {
Alert alert = driver.switchTo().alert(); 

        driver.switchTo().alert(); 

        alert.accept(); 
}

catch (org.openqa.selenium.NoAlertPresentException e){
 System.out.println("No alert present");      
}

暂无
暂无

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

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