如何使用Java在selenium Webdriver中单击弹出窗口上的allow按钮

时间:2017-11-24 04:20:11

标签: java selenium firefox selenium-webdriver popup

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

enter image description here

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

错误

线程“main”中的异常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',os.name:'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:normal,platform:MAC,platformName:MAC,platformVersion:16.7.0,rotate:false,timeouts:{implicit:0,pageLoad:300000,script:30000}} 会议ID:3cb929c6-a392-b247-a6ec-72af24f1ab6f     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)     at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)     at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)     在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)     在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)     在org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)     在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)     在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:657)     在org.openqa.selenium.remote.RemoteWebDriver $ RemoteTargetLocator.alert(RemoteWebDriver.java:986)     在com.toolbar.pages.FFInstallationpage.main(FFInstallationpage.java:50)

1 个答案:

答案 0 :(得分:0)

如果您预计会有警报但有时可能无法打开。您可以使用trycatch

来处理
try {
Alert alert = driver.switchTo().alert(); 

        driver.switchTo().alert(); 

        alert.accept(); 
}

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