简体   繁体   中英

How can I assign string copied to clipboard after selenium button click to a variable in C#?

I want to make string copied from selenium button click. The site provides a button-like div tag. If I click the tag, then an URL copied to my clipboard. And I want that URL into string. Can Selenium make that possible? here's some code:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
...
_driverService = ChromeDriverService.CreateDefaultService();
_driverService.HideCommandPromptWindow = true;

_options = new ChromeOptions();
_options.AddArgument("disable-gpu");
_options.AddArgument(String.Format("user-data-dir={0}", profilePath));
// _options.AddArgument("headless");
// headless cannot usable in WPF... why?

_driver = new ChromeDriver(_driverService, _options);

_driver.Navigate().GoToUrl("The site that provides what I want");
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

var element = _driver.FindElementByXPath("The tag's xpath");
element.Click();
// I think this copy an url probably, or not.
// what should I do from here? to assign that url to a string variable?

Use selenium to create a text area element, and paste the contents of the clipboard. Then extract the value from the text area.

// After clicking the copy-to-clipboard button

var executor = (IJavaScriptExecutor)driver;

var textarea = (IWebElement)executor.ExecuteScript("document.body.appendChild(document.createElement('textarea'));");

var action = new Actions(driver)
    .MoveToElement(textarea)
    .Click()
    .KeyDown(OpenQA.Selenium.Keys.Control)
    .SendKeys("v")
    .KeyUp(OpenQA.Selenium.Keys.Control);
    
action.Perform();

var url = textarea.GetAttribute("value");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM