dari manas
dari manas

Reputation: 23

how to copy to clipboard text between h2 tags in selenium python

what i try to do here is get email code for verification. so I log in to the email, select and copy the 6 digits code from the email and paste it to the other tab. everything is done except i can not double click to select the 6 digit code and copy it to clipboard. the code is between h2 tag and nothing else, like this: 639094 where 639094 is actually the code which i need to be copied. how can i find the code element or whatever and copy it? here is a screen shot of the email and the chrome inspect element if anything helps.

this is the code that I use to copy the code:

codeID = driver.find_element(By.XPATH, 
'//table[@class="main"]//tr//td//p//h2').text

ActionChains = ActionChains(driver)
ActionChains.double_click(codeID).perform()
time.sleep(2)
codeID.send_keys(Keys.CONTROL + 'c')
text = pyperclip.paste()
print(text)

screen shot

Upvotes: 0

Views: 2353

Answers (3)

dari manas
dari manas

Reputation: 23

element is found however looks like can not be copied. the error is Element is not reachable by keyboard. if i do everything automatically up until the element is selected with double click and copy the element with my actual keyboard the element is copied, however when selenium try to copy i get the error from above. the code i use to double click the element is:

codeID = driver.find_element(By.XPATH, '//*[@id="message-htmlpart1"]/div/table/tbody/tr/td[2]/div/table/tbody/tr/td/table/tbody/tr/td/h2')
ActionChains = ActionChains(driver)
ActionChains.double_click(codeID).perform()
time.sleep(2)

and to do the copy is :

codeID.send_keys(Keys.CONTROL + 'c')
text = pyperclip.paste()
print(text)

this is the part where the error ocur:

codeID.send_keys(Keys.CONTROL + 'c')
text = pyperclip.paste()
print(text)

for some reason it says "Element is not reachable by keyboard" but the element/code numbers are selected.

if I use print(text) they are also printed in the console.

Upvotes: 1

iheb athimni
iheb athimni

Reputation: 25

Hey i will analyse this problem with you For the first part :

  • try to take that XPath you have and past it in the Xpath helper (google chrome extension) => If you find that element , than the problem in your code => if you don’t than the element is already in a frame or in a table The solution is to change your drive to the new frame and relocate the element inside the frame Exemple :

iframe_xpath = driver.find_element_by_xpath('//iframe')

driver.switch_to.frame('iframe_xpath') Now try to relocate the element starting from the iframe

For the second part : You say it’s a table so you need to mention the /td[i] and /tr[j] value where the number is located so you can get it Exemple

d = driver.find_element_by_xpath( "//tr[i]/td[j]").text

I hope that’s help

Upvotes: 0

rick
rick

Reputation: 305

driver.find_element_by_xpath('//table[@class="main"]//tr//td//h2').text this will give you the text/code

enter image description here

Upvotes: 0

Related Questions