paste url from clipbaord to browser address bar

2019-08-18 18:58发布

问题:

I need to copy url and paste it to browser address bar. Unfortunately when I copy url there is no data attribute from where I can getText and paste.

I used actions class as below to paste the url. But doesn't seem to work.

HMTL code:

  <li class="copyLink">
  <span class="link">Copy link</span>
  <input class="input" readonly="">
 </li> 

await browser.executeScript("window.open(arguments[0], '_blank')"); // opens new tab await browser.actions().keyUp(protractor.Key.CONTROL).perform();//to paste in the address bar await browser.sleep(1000);

Any suggestions on whats wrong with the code ?

Thanks

回答1:

UPDATED CODE AGAIN:

You were very close in your initial attempt. Using the sendKeys method you can send the current copied code in the clipboard to the current selected element using

await browser.actions().sendKeys(protractor.Key.CONTROL, 'v').perform();

You were missing the 'v' which, as I'm sure your aware, is the button you need on the keyboard after the control button is pressed.

This is where you can read more about sending specific key presses with Protractor

Latest Update:

If you address bar is not focused then the above approach will not work and I have not found a way to manually focus the address bar at this time. Another approach you could try is to use an npm package like clipboardy. This will allow you to copy to clipboard contents to a variable and then use a browser.get() to reach the required URL.

Install clipboardy package

npm i clipboardy

In your test

const clipboardy = require('clipboardy');

//create variable with value from clipboard
let urlFromClipboard= await clipboardy.read();

await browser.get(urlFromClipboard);


标签: protractor