可以将超链接文本复制到剪贴板中并写入文本框吗?

2024-03-28 17:47:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用chromedriver、selenium和pyautogui自动化一个在web上执行的任务。我可以在网页上打开一个弹出窗口(我相信它使用javascit)。然后我可以删除文本框中的现有文本,并向其中写入新文本,然后单击“发送”。(请原谅我的无知,因为我对一般编程非常陌生。这实际上是我的第一个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

Tags: thetonamedivsendmessagebytime
1条回答
网友
1楼 · 发布于 2024-03-28 17:47:12

payLink = driver.find_elements_by_xpath('//*[@id="clientEmailFormOneModal"]/div[3]/div[11]/div[2]/div/div[1]/div[2]/a')

这个返回webelements。要用字符串连接的字符串:

message_box.send_keys('hey', contactName, 'here is the link to pay:', payLink)

它不起作用,因此expected str instance, WebElement found。你知道吗

从webelement中提取值,如payLink[0].get_attribute("href")。你知道吗

相关问题 更多 >