簡體   English   中英

通過右鍵單擊並選擇“在新選項卡中打開鏈接”在新選項卡中打開鏈接不起作用(Selenium)

[英]Opening a link in a new tab by right clicking and selecting “Open link in new tab” is not working (Selenium)

我在 Chrome 上打開 Amazon,選擇 Appliance,輸入“whirlpool”,然后嘗試正確 select 並在新選項卡中打開“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();

如何才能右鍵單擊“在新選項卡中打開鏈接”?

您可以使用 org.openqa.selenium.Keys 和鍵盤快捷鍵(CONTROL + SHIFT + 單擊元素)在同一瀏覽器 window 的新選項卡中打開鏈接;

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

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

在此處查看 Keys 文檔

查看教程以獲取更多使用快捷鍵的示例;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM