我尝试此代码并打开mozila firefox获取google.com网站但从未打开新标签 为什么呢?
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://google.com");
IWebElement element = driver.FindElement(By.TagName("Body"));
System.Threading.Thread.Sleep(6000);
element.SendKeys(OpenQA.Selenium.Keys.Control + "t");
}
}
}
并尝试此操作,但它从未打开过新标签!
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://google.com");
var action = new Actions(driver);
System.Threading.Thread.Sleep(6000);
action.KeyDown(Keys.Control).SendKeys("t").Perform();
}
}
}
答案 0 :(得分:1)
您可以使用javascript执行程序: -
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open('https://www.google.com','_blank');");
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://google.com");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");
}
}
}
请参阅此链接: - How to handle the new window in Selenium WebDriver using Java?
答案 1 :(得分:0)
找到body
,然后找到send
个键。
static void Main(string[] args) {
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("https://www.google.com")
}