弹出窗口中的C#硒警报控件

时间:2020-06-15 22:08:10

标签: c# html selenium controls popupwindow

弹出窗口中的c#硒警报控件


如何控制控件在弹出窗口中发出警报?
弹出窗口= window.open

我尝试了几种方法,但最终都失败了。


测试代码html和C#

index.html

<button onclick="TEST();">TEST</button>
<div id="test">Wait</div>
<script>
 function TEST() {
  var win = window.open("test.html",
   "TEST",
   "location=no,width=600,height=500,status=no,resizable=yes,scrollbars=1,location=0"
  );

  win.focus();

  document.getElementById("test").innerText = "OK"; // << i want get text 

 }
</script>

test.html

<script>
    alert("TEST");
    self.close();
</script>

C#

    ChromeDriverService _driverService = ChromeDriverService.CreateDefaultService();
    //_driverService.HideCommandPromptWindow = true;

    ChromeOptions _options = new ChromeOptions();
    //_options.AddArgument("headless");
    //_options.AddArgument("no-sandbox");
    //_options.AddArgument("disable-gpu");

    ChromeDriver _driver = new ChromeDriver(_driverService, _options);
    _driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(3);
    _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
    _driver.Navigate().GoToUrl(@"file:///C:\Users\root\Desktop\새 폴더 (4)\index.html");

    IWebElement Element;
    IAlert alert;

    Element = _driver.FindElementByXPath("//button");
    Element.Click();
    // So far OK
    // no control popup alert

    // STEP 1 :: Error
    if(false)
    {
        alert = _driver.SwitchTo().Alert(); // << OpenQA.Selenium.NoAlertPresentException: 'no such alert  (Session info: chrome = 83.0.4103.97)'
        alert.Accept();
    }

    // STEP 2 : Error
    if(false)
    {
        _driver.SwitchTo().Window(_driver.WindowHandles.Last()); // << OK
        //_driver.Close(); // not working
        alert = _driver.SwitchTo().Alert(); // << OK
        alert.Accept(); // No Error No Run... I click directly. ok button in alert -> after error message  OpenQA.Selenium.NoSuchWindowException: 'no such window: target window already closed
        alert.SendKeys(OpenQA.Selenium.Keys.Return); // no run.. It is still at alert in popup window
        alert.Dismiss(); // no run.. It is still at alert in popup window
    }

    // STEP 3 : Error
    //_driver.SwitchTo().ActiveElement().Click(); // timeout Error

    // STEP 4 : Error
    //WebDriverWait wait = new WebDriverWait(_driver, new TimeSpan(10));
    //alert = wait.Until(ExpectedConditions.AlertIsPresent()); // timeout error
    //alert.Accept();

我尝试了很多事情,但无法控制test.html。
我想在popupwindow(test.html)中控制关闭警报
但是,我想控制弹出窗口而不使用下面的内容。

window.open hooking X       ex(window.open = function ...)
window.open xhr or fetch X    ex( fetch( popup url (test.html) )
winapi X                             ex( SendMessage WM_LBUTTONDOWN  or mouse_event )
alert hooking no time X        ex( alert = function....  However, since the popup floats immediately, there is no time to hook.)
i only popup alert control

有正常的弹出控制方法吗?

0 个答案:

没有答案