Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Why am I not able to get rid of a context menu with Selenium or brute force Javascript

I have tried just about everything, but I am not able to get rid of the context menu. Steps

  1. Right click context menu.
  2. Accept alert
  3. Try to get rid of the context menu.

Manually then context menu goes away, but not via automation. Here is the code. Trying to understand what is happening under the hood. Any automation tool/language for an explanation is ok.

        By contextMenuBox = By.cssSelector("div#hot-spot");
        By contextMenuTitle = By.xpath("//h3[text()='Context Menu']");
        By body = By.xpath("//html");
        
        WebDriver driver = DriverManager.getDriver();
        driver.get("https://the-internet.herokuapp.com/context_menu");
        driver.manage().window().maximize();
        
        String windowHandle = driver.getWindowHandle();
        
        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement contextEle = 
                wait.until(ExpectedConditions.visibilityOfElementLocated(contextMenuBox));
        
        Actions action = new Actions(driver);
        action.moveToElement(contextEle).contextClick().build().perform();
        
        Thread.sleep(1000); //Just a Debug step
        
        Alert alert = driver.switchTo().alert();
        alert.accept();
        
        driver.switchTo().window(windowHandle);
        
        System.out.println(driver.findElement(contextMenuTitle).getText());
        
        Thread.sleep(5000); //Just a Debug step

        /*
         * Trying to click using javascript.
         * element.click() as well as creating a mouse click event on the element and then
         * dispatching
         */
        SeleniumUtility.click(body,0,0); 
        driver.findElement(body).click();
        Actions newAction = new Actions(driver);
        newAction.moveToElement(driver.findElement(contextMenuTitle)).click().build().perform();
        newAction.sendKeys(Keys.ESCAPE).perform();
        
        Thread.sleep(2000); //Just a Debug step

Update - The only thing that works is the use of the Robot class

    Robot robot3 = new Robot();
    robot3.keyPress(KeyEvent.VK_ESCAPE);
    robot3.keyRelease(KeyEvent.VK_ESCAPE);

Does anyone have an explanation as to why Selenium and Javacript under the hood stuff don't work?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda