I'm trying to automate a test for which I need to search a movie, right-click on a link and select the first option (open in new tab). Have tried initiating a robot class, using the action class, treating the right-click selection window as a separate window, and even sending a "shift enter" command after selecting the link. None of it worked. the most popular solution (the action class) had the problem that even though I was able to right-click the link by, using a context-click when I gave the command to "press the down key of the keyboard" instead of moving into the right-click menu, it scrolled by webpage down, i.e. it did not interact with the menu at all. Although when I manually click the down button, it obviously selects the first option of the menu. (Have tried multiple Keys.DOWN commands as well) Here's my action class code:
driver.get("https://www.google.com");
WebElement search = driver.findElement(By.name("q"));
search.click();
search.sendKeys("After life" ENTER);
WebElement toClick =driver.findElement(By.xpath("//h3[contains(text(),'After Life (TV Series 2019–2022) - IMDb')]"));
Actions actions = new Actions(driver);
actions.contextClick(toClick).perform();
actions.sendKeys(Keys.DOWN).sendKeys(ENTER).perform();
CodePudding user response:
Maybe is makes sense to get "href" attribute, open a tab manually and go to the link on the new tab?
CodePudding user response:
I am not sure why it is not working with Actions
class and I assumed you are using Keys.DOWN
instead of keys.ARROW_DOWN
. Even it is also not working with keys.ARROW_DOWN
. By using both Actions
and Robot
classes it worked.
Code:
Actions actions = new Actions(driver);
actions.contextClick(toClick).perform();
Thread.sleep(2000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);