代码之家  ›  专栏  ›  技术社区  ›  Leder

在seleniumjava:无法切换到页面上显示的模式role=“dialog”

  •  0
  • Leder  · 技术社区  · 1 年前

    我在这里打开页面: page

    我无法点击cookie对话框!

    我也看不到iframe或警报窗口。我试过了 getWindowHandles 没有成功。

    这是我的硒测试:

    package org.leder;
    
    
    import dev.failsafe.internal.util.Assert;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Wait;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    import java.time.Duration;
    import java.util.Iterator;
    import java.util.Set;
    
    public class MainTest {
    
        public static final String ALDI_ONLINESHOP_GUTES_FÜR_ALLE = "ALDI ONLINESHOP - Gutes für alle";
    
        @Test
        public void loginLogoutTest() {
            WebDriver driver = new ChromeDriver();
    
            Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(15));
    
            driver.get("https://aldi-onlineshop.de");
    
            String title = driver.getTitle();
    
            System.out.println(title);
    
            Assert.isTrue(title.equals(ALDI_ONLINESHOP_GUTES_FÜR_ALLE), "Title is valid");
    
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("data-testid='uc-default-wall'")));
    
    
            String parent = driver.getWindowHandle();
    
            Set<String> s = driver.getWindowHandles();
    
    
            // Now iterate using Iterator
            Iterator<String> I1 = s.iterator();
    
            driver.switchTo().window(I1.next());
    
    
            WebElement submitButton = driver.findElement(By.cssSelector("data-testid='uc-accept-all-button'"));
    
    
            submitButton.click();
    
            //switch to the parent window
            driver.switchTo().window(parent);
    
            WebElement loginMenu = driver.findElement(By.cssSelector("[data-attr-value='My Account']"));
    
            loginMenu.click();
    
    
    
            driver.quit();
        }
    }
    

    我希望模式对话框被选中,按钮被按下。我在模态对话框的css值上得到一个错误:

    org.openqa.selenium.JavascriptException: javascript error: {"status":32,"value":"An invalid or illegal selector was specified"}
      (Session info: chrome=123.0.6312.122)
    Build info: version: '4.20.0', revision: '866c76ca80'
    System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.26-1-MANJARO', java.version: '17.0.10'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Command: [4ffbffb982d379cb84581e3add5c448d, findElement {value=data-testid='uc-default-wall', using=css selector}]
    Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 123.0.6312.122, chrome: {chromedriverVersion: 123.0.6312.122 (31f8248cdd9..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:40857}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:cdp: ws://localhost:40857/devtoo..., se:cdpVersion: 123.0.6312.122, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
    Session ID: 4ffbffb982d379cb84581e3add5c448d
    
    

    这个

    driver.switchTo().alert().accept();

    给这个

    org.openqa.selenium.NoAlertPresentException: no such alert

    1 回复  |  直到 1 年前
        1
  •  1
  •   JeffC    1 年前

    问题是您的两个CSS选择器无效。这就是错误的原因,例如。

    data-testid='uc-default-wall'
    data-testid='uc-accept-all-button'
    

    应置于括号内,例如。

    [data-testid='uc-default-wall']
    [data-testid='uc-accept-all-button']
    

    您的最后一个CSS选择器是有效语法。


    建议。。。

    1. 不要以变量的名称命名,例如。

      public static final String ALDI_ONLINESHOP_GUTES_FÜR_ALLE = "ALDI ONLINESHOP - Gutes für alle";
      

      而是以它所代表的内容命名,如主页标题。

      public static final String HOME_PAGE_TITLE = "ALDI ONLINESHOP - Gutes für alle";
      
    2. 不使用 Assert.isTrue() 以检查两个值是否为真。它旨在检查 boolean ,例如。

      boolean isTrue = true;
      Assert.assertTrue(isTrue, "It's true");
      

      当比较两个值时,如本例中比较两个字符串时,请使用 Assert.assertEquals() ,例如。

      Assert.assertEquals(driver.getTitle(), home_page_title, "Title is valid");
      

      这样,如果页面标题发生更改,您将收到如下消息

      Title is valid expected [ALDI ONLINESHOP - The old title] but found [ALDI ONLINESHOP - Gutes für alle]
      

      而不是

      Title is valid expected [true] but found [false]
      

      这是没有用的。