0

Suppose you copy a link by clicking on the button and you want to open that copied link in a new tab, then how to perform this operation in selenium C#.

driver.FindElement(By.Id("button")).click(); -----from this operation we just copy the link.

Question: Now How to open this copy link in new tab ?

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • 2
    I could help you, but since I see you are not accepting / upvoting received answers I will not. I'm sorry. @cruisepandey – Prophet Mar 20 '22 at 15:37
  • 2
    @Prophet: I agree with you. – cruisepandey Mar 20 '22 at 15:37
  • 2
    @Ravi: folks here do not appreciate it if you do not accept/upvote the helpful answer. See What should I do when someone answers my question? - https://stackoverflow.com/help/someone-answers – cruisepandey Mar 20 '22 at 15:39
  • Sorry for that, But I'm new in stackoverflow. It take sometime to understand how to upvoting and accepting but from now, I can do this. thanku for addressing me. @cruisepandey – Ravi Pratap Singh Mar 20 '22 at 16:28

2 Answers2

1

After getting the URL link with code like this:

var url = driver.FindElement(By.Id("button")).Text;

You can open a new tab and switch to it with

((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());

And then open the previously saved url there with driver with

driver.Url = url;

So that the entire code could be something like this:

var url = driver.FindElement(By.Id("button")).Text;
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Url = url;

In case you are getting the URL by clicking on that element as you mentioned driver.FindElement(By.Id("button")).click(); so that the URL is copied into the clipboard, you can use code like this:

using System.Windows.Forms;
driver.FindElement(By.Id("button")).click();
string url = Clipboard.GetText()
((IJavaScriptExecutor)_chromeDriver).ExecuteScript("window.open('" + url +"');");
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • 1
    You did not include `clipboard` in the first place. You shouldn't have really edited your answer with a clipboard solution in my opinion. – cruisepandey Mar 20 '22 at 17:09
  • 1
    I'm not sure I understand what's wrong with my solution. My answer contains 2 parts: first is about extracting the text from the web element. There is no use of clipboard there at all. The second solution includes use of clipboard and based on this answer https://stackoverflow.com/a/50820587/3485434. I guess it is correct – Prophet Mar 20 '22 at 17:29
1

Copied link will be present in the clipboard, and once you've opened a new tab using this:

driver.SwitchTo().NewWindow(WindowType.Tab);

You could just do this:

driver.Navigate().GoToUrl(Clipboard.GetText());

if you are facing

The name 'Clipboard' does not exist in the current context

after that line of code, you would have to import:

using System.Windows.Forms;

This also could result in below fault:

The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?

for that you'll have to go inside your project directory and then go to the project file and add

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

this should solve the issue.

#Approach 2:

((IJavaScriptExecutor)driver).ExecuteScript("navigator.clipboard.readText().then(text => window.location.replace(text));");
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Hi, I'm facing this Clipboard issue. Not able to import to using System.Windows.Forms. and not understand where I can use this code: netcoreapp3.1 true true – Ravi Pratap Singh Mar 21 '22 at 04:43
  • 1
    You should first go to your project folder (file explorer) there you would see one project file csproj file, open that in notepad and then go to `PropertyGroup` you already should see `TargetFramework` and you should add these two in them `true true` – cruisepandey Mar 21 '22 at 07:20
  • 1
    @RaviPratapSingh: May I know the reason for unacceptance? – cruisepandey Mar 22 '22 at 04:49
  • After adding this two true true in targetframe, my code was crupted and not running. – Ravi Pratap Singh Mar 28 '22 at 04:46