how to do right click and after that select copy option from the right click menu using down arrow keys in selenium python

907 views Asked by At

as the subject says, i want to open web page, double click on word, after that once the word is selected to do mouse right click and select copy from the menu. till now i have the following code which do everything, open google, click on word, invoke the mouse right click but i have no idea how to do/use down arrow key to select copy. here is the code:

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()

driver.get("https://www.google.com/?hl=en")

codeID = driver.find_element(By.XPATH, '//*[@id="SIvCob"]')

ActionChains = ActionChains(driver)
ActionChains.double_click(codeID).perform()

ActionChains.context_click(codeID).perform();

i found some solutions in java and tried to implement those in python, but non of those worked, for example as this one (which I tried to use as a replacement for the last line of my code):

from selenium.webdriver.common.keys import Keys
ActionChains.context_click(codeID).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).perform();

please if someone can help me how to do this. thanks!

1

There are 1 answers

0
dari manas On

so in case anyone have the problem, this worked for me

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').text
pyperclip.copy(codeID)

and the code was copied in the clipboard.