右键单击并选择“在新选项卡中打开链接”,无法在新选项卡中打开链接(Selenium)

时间:2020-05-14 16:32:19

标签: selenium selenium-webdriver selenium-chromedriver

我要在Chrome上启动Amazon,选择Appliance,输入“漩涡”,然后尝试右键选择并在新标签页中打开“ Whirlpool W10346771A Fresh Flow Produce Preserver”)“

              //| open | https://www.amazon.com/ | 
             driver.Navigate().GoToUrl("https://www.amazon.com/");

             // maximize window
             driver.Manage().Window.Maximize();
             IWebElement ddSearch = driver.FindElement(By.Id("searchDropdownBox"));

            // 3 | select | id=searchDropdownBox | label=appliances
            var selectTest = new SelectElement(ddSearch);

             // Select a value from the dropdown                
            selectTest.SelectByText("Appliances");       

            // find the search textbox
            IWebElement txtSearch = driver.FindElement(By.Id("twotabsearchtextbox"));

            // enter "Whirlpool" search text and submit the page
            txtSearch.SendKeys("whirlpool");
            txtSearch.Submit();

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
            wait.Until(e => e.FindElement(By.LinkText("Whirlpool W10346771A Fresh Flow Produce Preserver")));
            IWebElement lnkWhirlpool = driver.FindElement(By.LinkText("Whirlpool W10346771A Fresh Flow Produce Preserver"));


            // right click on the link and open the link in a new tab
            Actions actions = new Actions(driver);
            actions.MoveToElement(lnkWhirlpool);
            actions.ContextClick(lnkWhirlpool).SendKeys(Keys.ArrowDown).SendKeys(Keys.Enter).Build().Perform();

如何右键单击“在新标签页中打开链接”?

1 个答案:

答案 0 :(得分:0)

您可以使用org.openqa.selenium。键和键盘快捷键(CONTROL + SHIFT +单击元素)在同一浏览器窗口的新标签页中打开链接;

   WebElement lnkWhirlpool = driver.FindElement(By.LinkText("linkText_to_target_element"));

   String keyPress = Keys.CONTROL.toString() + Keys.SHIFT.toString() + Keys.ENTER.toString();
   lnkWhirlpool.sendKeys(keyPress);

检查键文档here

查看this教程以获取更多使用快捷键的示例;