我要在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();
如何右键单击“在新标签页中打开链接”?
答案 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教程以获取更多使用快捷键的示例;