Python 是否可以将超链接文本复制到剪贴板并将其写回文本框?

Python 是否可以将超链接文本复制到剪贴板并将其写回文本框?,python,selenium,selenium-chromedriver,pyautogui,Python,Selenium,Selenium Chromedriver,Pyautogui,我正在使用chromedriver、selenium和pyautogui自动化在web上执行的任务。我能够在网页上打开一个弹出窗口(我相信它使用javascipt)。然后,我可以删除文本框中的现有文本,并向其中写入新文本,然后单击“发送”。(请原谅我的无知,因为我对一般编程非常陌生。这实际上是对我的第一个python脚本/程序的扩展/编辑,这实际上很管用。) 当我最初打开弹出窗口编写消息时,文本框中存在一个模板。它包含一个重要且独特的超链接。我想将其提取出来,然后删除文本框中的所有内容,并编写一

我正在使用chromedriver、selenium和pyautogui自动化在web上执行的任务。我能够在网页上打开一个弹出窗口(我相信它使用javascipt)。然后,我可以删除文本框中的现有文本,并向其中写入新文本,然后单击“发送”。(请原谅我的无知,因为我对一般编程非常陌生。这实际上是对我的第一个python脚本/程序的扩展/编辑,这实际上很管用。)

当我最初打开弹出窗口编写消息时,文本框中存在一个模板。它包含一个重要且独特的超链接。我想将其提取出来,然后删除文本框中的所有内容,并编写一条包含该特定链接的新消息。此任务将自动执行多次,并且链接是唯一的,具体取决于消息将发送给谁

我曾尝试通过xpath和其他一些方法来查找超链接,但我不断遇到各种错误。有一件事我会注意到,并原谅我缺乏知识,但xpath在这个网站上看起来非常不同。同样,我相信这是因为javascript。下面是超链接的xpath示例:

//*[@id="clientEmailFormOneModal"]/div[3]/div[11]/div[2]/div/div[1]/div[2]/a
我在这个网站上使用了xpath来做其他事情,但是当这些div开始发挥作用时,事情变得很奇怪

以下是我当前执行此特定任务的功能:

def email_estimate():
    driver.find_element_by_id('hlEmailInvoiceOrEstimate').click() #clicks button on page to reveal the 'send an email' pop up
    time.sleep(1.9)
    driver.find_element_by_class_name('trumbowyg-editor').click() #clicks into the textbox on the pop up
    time.sleep(1)
    payLink = driver.find_elements_by_xpath('//*[@id="clientEmailFormOneModal"]/div[3]/div[11]/div[2]/div/div[1]/div[2]/a') #my attempt at finding the hyperlink by xpath and saving it as a variable
    time.sleep(.5)
    driver.find_element_by_class_name('trumbowyg-editor').clear() #clear the content of the textbox
    message_box = driver.find_element_by_class_name('trumbowyg-editor')
    message_box.send_keys('hey', contactName, 'here is the link to pay:', payLink) #Write message including the contact name which was pulled in a step before this and works as expected, and the hyperlink, which does not work
    time.sleep(30)
    print("Followed up on estimate "+ str(j) + ". Contact: " + contactName) #clicks the send button to send the message
    time.sleep(1.85)
您可以在上面的代码中看到作为步骤注释编写的预期结果。实际发生的是回溯和类型错误:

Traceback (most recent call last):
  File "C:/Users/myname/Desktop/automation/WIP/Payment FollowUps/payments.py", line 77, in <module>
    email_estimate()
  File "C:/Users/myname/Desktop/automation/WIP/Payment Follow Ups/payments.py", line 33, in email_estimate
    message_box.send_keys('hey', contactName, 'here is the link to pay:', payLink) #, contactName, payLink
  File "C:\Users\myname\Desktop\automation\WIP\Payment Follow Ups\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 478, in send_keys
    {'text': "".join(keys_to_typing(value)),
TypeError: sequence item 32: expected str instance, WebElement found
回溯(最近一次呼叫最后一次):
文件“C:/Users/myname/Desktop/automation/WIP/Payment FollowUps/payments.py”,第77行,在
电邮()
文件“C:/Users/myname/Desktop/automation/WIP/Payment Followers/payments.py”,第33行,在电子邮件中
消息框。发送密钥('hey',contactName',这是支付链接:',payLink)#,contactName,payLink
文件“C:\Users\myname\Desktop\automation\WIP\Payment followers\venv\lib\site packages\selenium\webdriver\remote\webelement.py”,第478行,在发送键中
{'text':“”.join(键到键入(值)),
TypeError:序列项32:应为str实例,找到WebElement

payLink=driver。通过xpath('/*[@id=“clientEmailFormOneModal”]/div[3]/div[11]/div[2]/div/div[1]/div[2]/a')查找元素。

这一个返回webelements。要用字符串连接这些元素:

消息框。发送密钥('hey',contactName',这是支付链接:',payLink)

这不起作用,因此找到了
预期的str实例WebElement

从Web元素中提取值,如
payLink[0]。获取属性(“href”)