Selenium PYTHON:在 gmail 中写电子邮件,但文本在签名下

Selenium PYTHON: write email in gmail but text goes under the signature

我正在编写一个脚本来登录 Gmail,打开一封新电子邮件并撰写它。 一切正常。这意味着我可以登录,按“撰写”按钮,填写收件人和主题框,还可以在主框中写下我的消息文本。

问题是文字写在我的签名 space 里面,而不是上面。
示例:

正文不在下面
.........
在这里我有一个签名
签名
正文写在这里格式为
签名

我尝试了几种解决方案,但我无法解决这个问题。唯一的方法是删除我的签名,但我会避免它。另一种方法是在Python找到主文本框时通过driver.implicitly_wait(5)冻结驱动程序,然后手动点击文本应该开始的位置。
我使用以下方法找到主框:

main_text = WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'div[aria-label="Message Body"]'))
)
print('main text found')
main_text.send_keys('text of the email')

如果有人知道如何告诉 Python 在哪里自动写入,我将不胜感激。
谢谢大家!

请尝试下面的代码,我已经在我的电脑上成功运行这个代码。

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.implicitly_wait(20)

driver.get('https://www.gmail.com/')
driver.find_element_by_xpath('//input[contains(@type,"email")]').send_keys("YourEmail")
driver.find_element_by_xpath('//span[text()="Next"]/parent::button').click()

time.sleep(5)
driver.find_element_by_xpath('//input[contains(@type,"password")]').send_keys("YourPassword")
driver.find_element_by_xpath('//span[text()="Next"]/parent::button').click()

# Time to load GMail fully
time.sleep(10)
driver.find_element_by_xpath('//div[contains(text(),"Compose")]').click()
driver.find_element_by_xpath('//textarea[contains(@name,"to")]').send_keys('To-EmailID')
driver.find_element_by_xpath('//input[contains(@name,"subjectbox")]').send_keys('Test Mail')

# Write Email
driver.find_element_by_xpath('//div[contains(@class,"gmail_default")]').send_keys("This is a messagebox\n")

# Send Message
driver.find_element_by_xpath('//div[contains(text(),"Send") and @role="button"]').click()

print("Message Sent Successfully")

几个小时后,我自己找到了一个方法,我将 post 在这里提供解决方案(以防其他人需要它)。我希望对我的问题的回答不会造成任何问题。

当我通过 Selenium 在 Gmail 中写一封电子邮件时,文本被添加到签名下方,位于主文本框的最底部并采用签名字体。 嗯,不是很有用...

我用来定位文本框对象的代码如下:

main_text = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, 'div[aria-label="Message Body"]'))
)                                            
                                                        
print('main text found') 
driver.implicitly_wait(3)  
main_text.send_keys('test text box')

或者像这样(从空白处复制 xpath space 正文应该去的地方):

main_text = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id=":nu"]'))
)                                            
                                                        
print('main text found') 
driver.implicitly_wait(3)  
main_text.send_keys('test text box')

不幸的是,其中 none 个或按名称、ID、CLASS_NAME 或 XPATH 的其他位置工作正常。

解决方案:
再试一次,我只检查了空白主文本框的第一行,我只使用了第一行的 xpath,而不是整个文本框:

main_text = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id=":rt"]/div/div/div/div/div/div/div/div/p[1]'))
)                                                                                       
print('main text found') 
driver.implicitly_wait(3)  
main_text.send_keys('Test line 1\n\nTest line 2\nTest line 3\n..\nTest via Selenium chromedriver')

终于把正文写在正确的位置,干净的在签名上方。