Jeremy Hunt
Jeremy Hunt

Reputation: 21

How to copy an element text, In Selenium python

my code is

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox()
browser.get('example.com')


sctitle = browser.find_elements_by_tag_name('h1')
scp = browser.find_elements_by_xpath("//article[@id='the-post']//p[3]")[2].text

the goal is selenium select and copy the h1 title tag, Than in a new tab paste in in the form. i have problem with the copying and pasting it. when i want to paste it, Nothing happends. i use this command for paste:

browser.execute_script("window.open('');")
browser.switch_to.window(browser.window_handles[2])
browser.get('https://www.sitea.com')
elem = browser.find_element_by_class_name('TextArea__textArea')
elem.send_keys(sctitle.text)

Upvotes: 1

Views: 675

Answers (1)

Amit
Amit

Reputation: 20506

Change the following to find only single element. find_elements returns a list so .text method doesn't work.

sctitle = browser.find_element_by_tag_name('h1')

Upvotes: 3

Related Questions