1

I'm trying to select a web attribute with python via webdriver, specifically I want to copy the name that I entered to the recipent, but I can't do.

enter image description here

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import time
driver = webdriver.Chrome('C:\chromedriver.exe')
driver.get('https://www.gmail.com/')
driver.implicitly_wait(5)
loginBox = driver.find_element_by_xpath('//*[@id ="identifierId"]')
loginBox.send_keys('xxxxxxxxxxxxxxxxxx')
nextButton = driver.find_elements_by_xpath('//*[@id ="identifierNext"]')
nextButton[0].click()
passWordBox = driver.find_element_by_xpath('//*[@id ="password"]/div[1]/div / div[1]/input')
passWordBox.send_keys('xxxxxxxxxxxxx')
nextButton = driver.find_elements_by_xpath('//*[@id ="passwordNext"]')
nextButton[0].click()
time.sleep(2)
driver.get('https://mail.google.com/mail/u/0/#inbox?compose=new')
driver.find_element_by_xpath('//textarea[1]').send_keys('Grecia Abad del toro')
driver.find_element_by_xpath('//textarea[1]').send_keys(Keys.ENTER)

How can I literally select and copy the text that is in the mail recipient? Thanks very much.

3
  • 2 questions: 1) why do you need to read the text you just entered into the element by yourself? 2) Are the last 2 lines in your code working correct? Commented Dec 9, 2021 at 15:39
  • It is an academic email. When you put the name of the person it throws you the mail and I need to copy them Commented Dec 9, 2021 at 21:11
  • Yes, the last two lines of the code work correctly. In fact all the code is functional, I just need to select it and copy it Commented Dec 9, 2021 at 21:13

1 Answer 1

1

This element is containing the contact email address:

//form[@method='POST']//span[@email]

So, to get the addressee email address you can do the as following:

driver.get('https://mail.google.com/mail/u/0/#inbox?compose=new')
driver.find_element_by_xpath('//textarea[1]').send_keys('Grecia Abad del toro')
driver.find_element_by_xpath('//textarea[1]').send_keys(Keys.ENTER)
time.sleep(2)
email = driver.find_element_by_xpath('//form[@method="POST"]//span[@email]').get_attribute("email")
print(email)
Sign up to request clarification or add additional context in comments.

2 Comments

File "gmail.py", line 41 email = driver.find_element_by_xpath('//form[@method='POST']//span[@email]').get_attribute("email") ^ SyntaxError: invalid syntax
Corrected that, sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.