【问题标题】:Selenium webdriver & powershell to highlight text from one element, copy it, and then pasteSelenium webdriver & powershell 从一个元素中突出显示文本,复制它,然后粘贴
【发布时间】:2014-10-22 16:03:31
【问题描述】:

所以我需要突出显示元素(textarea)中的文本,复制突出显示的文本,然后将其粘贴到另一个元素中。

存储第一个元素的值,然后使用 SendKeys 填充另一个元素对我来说不是一个选项。因为那样我会遇到一个 javascript 的其他问题。

这是一个关于我到目前为止已经走了多远的例子:

# Creating FirefoxDriver
$driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver

# Go to made up URL
$driver.url = "https://www.madeupdomain.com/"

# Find element and store in $MyElement
$MyElement = $driver.FindElementByXPath("//*[@id='MadeUpTextAreaElementId']")

# Attempt to highlight all
$MyElement.SendKeys($driver.keys.CONTROL + 'A')

# Attempt to copy text from text
$MyElement.SendKeys($driver.keys.CONTROL + 'C')

# Find another element to paste text to
$MyOtherElement = $driver.FindElementByXPath("//*[@id='AnotherMadeUpTextAreaElementId']"

# Attempt to paste copied text to another element
$MyOtherElement.SendKeys($driver.keys.CONTROL + 'V')

但这只会在第一个元素中输入“A”和“C”,然后在最后一个元素中输入“V”。

猜测问题出在“$driver.keys.CONTROL”部分。我如何让 Selenium 了解它是我想要执行的按键组合?

【问题讨论】:

    标签: powershell selenium


    【解决方案1】:

    原来我在“$driver.keys.CONTROL”这个问题上是错误的。

    "$driver.keys.CONTROL" 远不是解决方案!

    为了在 Powershell 中模拟 ctrl + a、ctrl + c 和 ctrl + v,您需要使用 System.Windows.Forms.SendKeys。可以在此处找到使用此功能的所有可用击键列表: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx

    其次,为了在 Selenium 网络驱动程序中使用这些击键,我使用了来自 OpenQA.Selenium.Interactions.Actions 的 SendKeys 方法。在查看了几个与 Java 相关的线程后,我发现了这一点。我花了一些时间来转换成 Powershell 友好的代码: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html#sendKeys(java.lang.CharSequence...)

    所以无论如何这里是我的代码更新,执行我需要的 ctrl + a、ctrl + c 和 ctrl + v 操作:

    # Creating FirefoxDriver
    $driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver
    
    # Create instans of Actions
    $actions = New-Object OpenQA.Selenium.Interactions.Actions($driver)
    
    # Go to made up URL
    $driver.url = "https://www.madeupdomain.com/"
    
    # Find element and store in $MyElement
    $MyElement = $driver.FindElementByXPath("//*[@id='MadeUpTextAreaElementId']")
    
    # Attempt to highlight all
    $actions.SendKeys($MyElement,[System.Windows.Forms.SendKeys]::SendWait("^a")) | out-null
    
    # Attempt to copy text from text
    $actions.SendKeys($MyElement,[System.Windows.Forms.SendKeys]::SendWait("^c")) | out-null
    
    # Find another element to paste text to
    $MyOtherElement = $driver.FindElementByXPath("//*[@id='AnotherMadeUpTextAreaElementId']"
    
    # Attempt to paste copied text to another element
    $actions.SendKeys($MyElement,[System.Windows.Forms.SendKeys]::SendWait("^v")) | out-null
    

    只需确保在使用 System.Windows.Forms.SendKeys 时您的浏览器窗口处于焦点位置!我在某些运行期间选择了我的 powershell 提示符,这导致了不必要的故障排除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 2021-02-17
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多