from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
import os
import urllib.parse
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import codecs
import random
from bs4 import BeautifulSoup
import requests
import time
import rpyc
import sys
import docker

headers = {
        "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
        "Content-Type": "application/x-www-form-urlencoded"
}


options = webdriver.ChromeOptions()
#options.add_argument("--proxy-server=socks5://172.104.67.159:8180")
#option.add_argument("--user-data-dir="+self.userdir+profilepath+"\\")

options.add_argument("--user-data-dir=C:\\Users\\jared\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 7\\")

#driver = webdriver.Remote(
#    command_executor='http://127.0.0.1:4444/wd/hub',
#command_executor='http://192.53.174.202:4444/wd/hub',
#command_executor='http://172.104.93.163:4444/wd/hub',
 
#command_executor='http://dev2.choozmo.com:14444/wd/hub',
#desired_capabilities=options.to_capabilities())
driver = webdriver.Chrome(options=options)


#desired_capabilities=DesiredCapabilities.CHROME)
driver.set_window_size(1400,1000)

driver.get('https://www.linkedin.com/mynetwork/')
time.sleep(5)

elmts=driver.find_elements_by_xpath("//ul[@class='artdeco-card mb4 overflow-hidden']//button[contains(@aria-label,'邀請')]")
#elmts=driver.find_elements_by_xpath("//ul[@class='artdeco-card mb4 overflow-hidden']//span[@class='artdeco-button__text']/..//button[contains(@aria-label,'邀請')")

#elmts=driver.find_elements_by_xpath("//span[contains(text(),'建立關係')]/..")

for elmt in elmts:
        print(elmt)
        i=random.randint(0,5)
        if i <=2:
                webdriver.ActionChains(driver).move_to_element(elmt).perform()
                webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
                time.sleep(3)


print(elmts)
print(len(elmts))

time.sleep(9999)
#https://whatismyipaddress.com/ip/61.230.75.30
#driver.get('https://whatismyipaddress.com/')
#fw=codecs.open('c:/tmp/gg.html','w','utf-8')
#fw.write(driver.page_source)
#fw.close()
#import sys
#sys.exit()
