-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathclipboard_action.py
75 lines (62 loc) · 2.58 KB
/
clipboard_action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
from __future__ import print_function
# SyntaxError: from __future__ imports must occur at the beginning of the file
# based on: https://qna.habr.com/q/786911
import clipboard
# https://pypi.org/project/clipboard/
# claims to work for Windows, Mac and Linux
# https://github.com/asweigart/pyperclip
import sys
from os import getenv
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
if getenv('OS') != None :
homedir = getenv('USERPROFILE').replace('\\', '/')
else:
homedir = getenv('HOME')
# alternatively PATH=%PATH%;%USERPROFILE%\Downloads
options = Options()
# NOTE: clipboard manipulation only works in visual mode
# options.add_argument('--headless')
# options.add_argument('--disable-gpu')
# NOTE:TypeError: __init__() got an unexpected keyword argument 'chromedriver'
# the 'chromedriver' is not a keyword arg here
driver = webdriver.Chrome(homedir + '/' + 'Downloads' + '/' + 'chromedriver', options = options)
driver.get("https://yandex.ru/chat/#/chats/1%2F0%2Fccb05ef5-1472-4e50-a926-602807a6ef94")
elements_xpath = '//div[contains(@class, "yamb-message-balloon")]'
WebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.XPATH, elements_xpath)))
elements = driver.find_elements_by_xpath(elements_xpath)
if (len(elements) == 0):
print('No elements found by xpath: {}'.format(elements_xpath), file = sys.stderr)
exit(1)
print('Found {} elements by xpath: {}'.format(len(elements), elements_xpath), file = sys.stderr)
try:
# object has no attribute 'clear'
clipboard.copy('')
except Exception, e:
print('Exception (ignored): {}'.format(e), file = sys.stderr)
pass
# a.k.a. Actions in java
actionChains = ActionChains(driver)
actionChains.context_click(elements[4]).perform()
get_link_text = 'Get message link'
driver.find_element_by_xpath("//span[text()='{}']/..".format(get_link_text)).click()
# only on Windows
try:
text = clipboard.paste() # text will have the content of clipboard
# except:
except Exception, e:
# Pyperclip could not find a copy/paste mechanism for your system.
# Possibly missing
# sudo apt-get install -qy xclip xsel
print('Exception (ignored): {}'.format(e), file = sys.stderr)
pass
if text != None and text != '':
print('Received through clipboard: {}'.format(text))
else:
print('Failed to copy/paste through clipboard', file = sys.stderr)
driver.quit()