如何在 selenium C# 的新选项卡中打开 Previously copy link?
How to open Previously copy link in the new tab in selenium C#?
假设你通过点击按钮复制了一个link并且你想在一个新的选项卡中打开复制的link,那么如何在selenium C#中执行这个操作。
driver.FindElement(By.Id("button")).click(); -----from this operation we just copy the link.
问题:现在如何在新标签页中打开此副本link?
使用如下代码获得 URL link 后:
var url = driver.FindElement(By.Id("button")).Text;
您可以打开一个新标签并使用
切换到它
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
然后打开之前保存的url那里有with
驱动
driver.Url = url;
所以整个代码可能是这样的:
var url = driver.FindElement(By.Id("button")).Text;
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Url = url;
如果您通过单击您提到的 driver.FindElement(By.Id("button")).click();
元素获得 URL,以便将 URL 复制到剪贴板,您可以使用如下代码:
using System.Windows.Forms;
driver.FindElement(By.Id("button")).click();
string url = Clipboard.GetText()
((IJavaScriptExecutor)_chromeDriver).ExecuteScript("window.open('" + url +"');");
复制的 link 将出现在剪贴板中,一旦您使用此打开新标签:
driver.SwitchTo().NewWindow(WindowType.Tab);
你可以这样做:
driver.Navigate().GoToUrl(Clipboard.GetText());
如果你面对
The name 'Clipboard' does not exist in the current context
在那行代码之后,您必须导入:
using System.Windows.Forms;
这也可能导致以下故障:
The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?
为此,您必须进入项目目录,然后转到项目文件并添加
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
这应该可以解决问题。
#方法二:
((IJavaScriptExecutor)driver).ExecuteScript("navigator.clipboard.readText().then(text => window.location.replace(text));");
假设你通过点击按钮复制了一个link并且你想在一个新的选项卡中打开复制的link,那么如何在selenium C#中执行这个操作。
driver.FindElement(By.Id("button")).click(); -----from this operation we just copy the link.
问题:现在如何在新标签页中打开此副本link?
使用如下代码获得 URL link 后:
var url = driver.FindElement(By.Id("button")).Text;
您可以打开一个新标签并使用
切换到它((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
然后打开之前保存的url那里有with
驱动driver.Url = url;
所以整个代码可能是这样的:
var url = driver.FindElement(By.Id("button")).Text;
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Url = url;
如果您通过单击您提到的 driver.FindElement(By.Id("button")).click();
元素获得 URL,以便将 URL 复制到剪贴板,您可以使用如下代码:
using System.Windows.Forms;
driver.FindElement(By.Id("button")).click();
string url = Clipboard.GetText()
((IJavaScriptExecutor)_chromeDriver).ExecuteScript("window.open('" + url +"');");
复制的 link 将出现在剪贴板中,一旦您使用此打开新标签:
driver.SwitchTo().NewWindow(WindowType.Tab);
你可以这样做:
driver.Navigate().GoToUrl(Clipboard.GetText());
如果你面对
The name 'Clipboard' does not exist in the current context
在那行代码之后,您必须导入:
using System.Windows.Forms;
这也可能导致以下故障:
The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?
为此,您必须进入项目目录,然后转到项目文件并添加
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
这应该可以解决问题。
#方法二:
((IJavaScriptExecutor)driver).ExecuteScript("navigator.clipboard.readText().then(text => window.location.replace(text));");