[英]Copy and paste the link from clipboard in selenium C#
I'm having the following issue:我遇到以下问题:
I'm saving the locators as我将定位器保存为
public static By CopyUrl = By.XPath("//a[@data-key='UrlLink']");
In the test, I cannot GetText()
, or store the whole CopyUrl
in a var
, since it's void
.在测试中,我不能
GetText()
或将整个CopyUrl
存储在var
中,因为它是void
。 Driver.Click(XPath.MainMenu.Tabs.CopyUrl);
How can I store the link which is saved in clipboard after I click on it, and paste it in a new tab.单击后如何存储保存在剪贴板中的链接,并将其粘贴到新选项卡中。
I tried:我试过了:
var elem =Driver.SwitchTo().NewWindow(WindowType.Tab).Navigate().GoToUrl("myCopiedUrl").ToString();
Driver.SwitchTo().NewWindow(WindowType.Tab).Navigate().GoToUrl(elem);
It is not working for you since both Click()
and Navigate().GoToUrl()
methods are returning void.它对您不起作用,因为
Click()
和Navigate().GoToUrl()
方法都返回无效。
In order to get the current URL you should use this method:为了得到当前的 URL 你应该使用这个方法:
String currentURL = driver.Url;
or要么
String currentURL = driver.getCurrentUrl();
or要么
String currentURL = driver.getLocation();
[Test, Order(01234)]
public void t_Document_CopyUrl_Downloaded()
{
NavigateToYourPage();
UploadNewDocument();
NavigateToTheSpecificTab();
//Click on "Copy URL"
string fileName = Driver.FindElement(XPath.Input).GetValue();
IWebElement copyUrl = Driver.FindElement(XPath.CopyUrl);
string currentUrl = copyUrl.GetAttribute("textContent");
Driver.Click(XPath.CopyUrl);
Driver.WaitForNoSpinner();
// Opens a new tab and switches to new tab
Driver.SwitchTo().NewWindow(WindowType.Tab);
Driver.Navigate().GoToUrl(currentUrl);
Driver.Navigate().GoToUrl("chrome://downloads/");
string downloadedFileName = Driver.ExecuteJavaScript<string>("return document.querySelector('downloads-manager').shadowRoot.querySelector('downloads-item').shadowRoot.querySelector('#name').textContent;");
Driver.SwitchTo().Window(Driver.WindowHandles[0]);
Assert.IsTrue(downloadedFileName.StartsWith(fileName));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.