尝试通过右键单击Web元素打开新选项卡但无法打开

时间:2018-05-06 02:24:12

标签: c# selenium selenium-webdriver

点击'衬衫'元素代替' Tshirt'。在右键单击后,它不会执行“打开新标签”'操作

注意:当右键单击窗口时,窗口会向下移动一点并选择“衬衫”。元件。

driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://www.flipkart.com");
IWebElement menHomePageElement = driver.FindElement(By.XPath("//a[@title = 'Men']"));
driver.Manage().Window.Maximize();

Thread.Sleep(5000);

driver.FindElement(By.XPath("//div/button[text() = '✕']")).Click();
Actions action = new Actions(driver);

IWebElement tshirtLinnk = driver.FindElement(By.XPath("//a[@title = 'T-Shirts']"));
IWebElement shirtLinnk = driver.FindElement(By.XPath("//a[@title = 'Shirts']"));
IWebElement kurtasLinnk = driver.FindElement(By.XPath("//a[@title = 'Kurtas']"));

Thread.Sleep(3000);

action.MoveToElement(menHomePageElement).Perform();

Thread.Sleep(3000);

action.MoveToElement(tshirtLinnk).Perform();

Thread.Sleep(5000);

action.ContextClick(tshirtLinnk).SendKeys(Keys.ArrowDown).Build().Perform();

Element Trying to click

Item getting clicked

2 个答案:

答案 0 :(得分:3)

你能用以下方法欺骗你吗?打开包含所需网址的新窗口:

driver.Url = "http://www.flipkart.com";      
driver.FindElement(By.CssSelector("button._2AkmmA._29YdH8")).Click();
Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(By.CssSelector("#container > div > header > div.zi6sUf > div > ul > li:nth-child(3) > a > span"))).Perform();
action.MoveToElement(driver.FindElement(By.CssSelector("a[title='T-Shirts']"))).Perform();

string nextLink = driver.FindElement(By.CssSelector("a[title='T-Shirts']")).GetAttribute("href");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open('" + nextLink +"');");

答案 1 :(得分:0)

使用Action和Sendkeys或Robot框架java API按 Ctrl + B 。 然后按索引切换到新选项卡并导航到所需的URL。