为什么我的弹出取消按钮不可点击?

时间:2018-06-27 09:53:26

标签: html selenium selenium-webdriver

我要处理此弹出窗口,然后单击“取消”按钮。我尝试过,但它不可点击

弹出式图片

Image For Popup

我还添加了HTML图片,请检查并告诉我解决方法

用于HTML代码的图像

Image For HTML Code

测试用例正在通过Selenium,但没有点击。向下,我还将附加Selenium代码。

    package Backendsite;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class SkipTest {
  @Test
  public void f() 
  {
      System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");

      //Setting To Open Incoginoto Window
      ChromeOptions options = new ChromeOptions();
      options.addArguments("-incognito");
      DesiredCapabilities capabilities = DesiredCapabilities.chrome();
      capabilities.setCapability(ChromeOptions.CAPABILITY, options);
      WebDriver chromedriver=new ChromeDriver(capabilities);

      chromedriver.manage().window().maximize();

      //Opening The WebSite
      chromedriver.get("xxxxxxxxxxxxxxxx");


      chromedriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      Pomsite p1 = PageFactory.initElements(chromedriver, Pomsite.class);

      Select s1 = new Select(p1.getE1());
      s1.selectByVisibleText("xxxxxxxxxx");

      p1.getE2().click();

      try
      {
      Actions a1 = new Actions(chromedriver);
      a1.moveToElement(p1.getE3()).click(p1.getE4()).build().perform();
      }
      catch(Exception e)
      {
          System.out.println("Can't Click");
      }

      chromedriver.close();
  }
}

我的第二个Pageobject Model类代码:

 package Backendsite;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class Pomsite 
{

    public WebElement getE1() {
        return e1;
    }

    public WebElement getE2() {
        return e2;
    }

    public WebElement getE3() {
        return e3;
    }

    public WebElement getE4() {
        return e4;
    }


    @FindBy(id="ddlstore")
    private WebElement e1;

    @FindBy(xpath="//input[@id='CustomPaging_GridView_gv_edit1_0']")
    private WebElement e2;

    @FindBy(xpath="/html/body/div[1]/div/div/div[3]")
    private WebElement e3;

    @FindBy(xpath="/html/body/div[1]/div/div/div[3]/button[2]")
    private WebElement e4;



}

我的用于取消按钮弹出的HTML代码

<div class="modal-footer">
                <span id="prcid" style="display:none;">processing...</span>
                <button type="button" id="skipok" onclick="skipoverall(this)" class="btn btn-primary" data-id="10514438996">Ok</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
            </div>

PS:我也尝试过警报和提示弹出窗口,它不起作用。

2 个答案:

答案 0 :(得分:0)

尝试一下:

@FindBy(xpath="//div[@class = 'modal-footer']/button[@data-dismiss = 'modal']")
private WebElement cancelButton;

Actions a1 = new Actions(chromedriver);
a1.moveToElement(p1.getCancelButton()).click(p1.getCancelButton()).build().perform();

PS我想我找到了问题,为什么您要移至E3,但单击E4?可以这样完成:

Actions a1 = new Actions(chromedriver);
a1.moveToElement(p1.getE4()).click(p1.getE4()).build().perform();

答案 1 :(得分:0)

请通过删除try-catch块尝试以下操作。

    WebDriverWait wait=new WebDriverWait(chromedriver,20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[text()='Cancel']")));
    chromedriver.findElement(By.xpath("//button[text()='Cancel']")).click();