5

I have an alert box that shows a URL in the text input area and copies the link to the clipboard. I want to be able to open a new tab, and paste the URL into the address bar.

I have tried :

var generatedLink = System.Windows.Forms.Clipboard.GetText();
_chromeDriver.Navigate().GoToUrl(generatedLink);

generatedLink returns ""

I have also tried :

Attempt 1:

((IJavaScriptExecutor)_chromeDriver).ExecuteScript("window.open();");
List<String> tabs = new List<String>(_chromeDriver.WindowHandles);
_chromeDriver.SwitchTo().Window(tabs[1]);           
_chromeDriver.FindElement(By.XPath("//body")).SendKeys(Keys.Control + "l");
_chromeDriver.FindElement(By.XPath("//body")).SendKeys(Keys.Control + "v");

SendKeys(Keys.Control + "l") does not select the address bar like it should

SendKeys(Keys.Control + "v") does not paste the text

I know that the text is copied because when I debug the test, I can paste the copied text into Notepad without any problems.

The following is where the copying occurs:

const url = window.location.host + urlPattern.stringify(newUrlParameters) + queryString; 
  (navigator as any).clipboard.writeText(url) 
    .then(() => window.prompt('Link copied to clipboard!', url)) 
    .catch(() => window.prompt('Copy to clipboard: Ctrl+C, Enter', url)); 
Deepak Rai
  • 2,163
  • 3
  • 21
  • 36
  • `generatedLink` shouldn't be empty if there's text on the clipboard. Can you set a breakpoint and verify your url is there? Using your first set of code it works for me, I had to include the "http://" part of the url. Using just "www.google.com" I get a `WebDriverException`, with "https://" in front of the url it works fine. – gunnerone Jun 12 '18 at 15:14
  • @gunnerone I set a breakpoint and am able to manually use Ctrl + V to paste the link into the address bar, but generatedLink is still empty. – Catherine McDowell Jun 12 '18 at 15:47
  • 3
    Possible duplicate of [Read Text from Clipboard](https://stackoverflow.com/questions/35867427/read-text-from-clipboard) – JeffC Jun 12 '18 at 16:24
  • Tried the similar question solution with no success – Catherine McDowell Jun 12 '18 at 17:58

2 Answers2

0

To open a new tab with the URL copied from the Clipboard you'll need to reference the namespace System.Windows.Forms and you can use the following solution:

using System.Windows.Forms;
// other code
string myURL = Clipboard.GetText()
((IJavaScriptExecutor)_chromeDriver).ExecuteScript("window.open('" + myURL +"');");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I have a reference to the namespace, as seen in the question above. But rather than returning the url from the clipboard, it is an empty string – Catherine McDowell Jun 12 '18 at 15:43
  • If the _URL_ is at all copied to the clipboard this code block will cater to your need. Seems _URL_ is not copied to the _clip board_. Can you update the question with the relevant code how you are handling the _Alert Box_ to analyze if the _url_ is copied properly? – undetected Selenium Jun 12 '18 at 15:48
0

If you want to set the url to the address bar, here are the solutions:

((IJavaScriptExecutor)Driver).ExecuteScript("navigator.clipboard.readText().then(text => window.location.replace(text));");

or use TextCopy package:

Driver.Navigate().GoToUrl(new TextCopy.Clipboard().GetText());
Vadim
  • 1
  • 1