from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.service import Service import requests import sys import json import dataset import random import os singles={} driver=None def click_btn(pathx): global driver elmts = driver.find_elements(By.XPATH, pathx) for elmt in elmts: webdriver.ActionChains(driver).move_to_element(elmt).perform() webdriver.ActionChains(driver).move_to_element(elmt).click().perform() def init_webdriver(portnum): options = webdriver.ChromeOptions() # options.add_argument("--headless") options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument('--ignore-certificate-errors') options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("useAutomationExtension", False) options.add_argument("--disable-gpu") options.add_argument("--disable-dev-shm-usage") # service = Service(executable_path="/usr/bin/chromedriver") # service = Service() # driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options) # driver = webdriver.Chrome(options, service) # driver = webdriver.Chrome( # options=options # ) driver = webdriver.Remote( command_executor='http://127.0.0.1:'+str(portnum)+'/wd/hub', options=options) str1 = driver.capabilities['browserVersion'] print('---') print(str1) print('---') # driver.set_window_size(1400,1000) # driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") return driver r = requests.get('http://cmm.ai:3001/api/push/YEiFOTMrUY?status=up&msg=OK&ping=') qs=['台北101有多高','觀景台營業時間','請問101觀景台可以帶寵物嗎','台北101有置物櫃嗎','觀景台可以使用美金付款嗎','請問停車收費方式','最近有什麼優惠','有素食餐廳嗎','美食街怎麼走','台北101樓層介紹','台北101美食','台北101品牌','台北101世界第幾高2024?','台北101幾號出口?','蓋101死了多少人?','台北101觀景台需要預約嗎?'] dockername='p4444' portnum=random.randint(4444,4555) print(portnum) os.system('docker container stop '+dockername) time.sleep(0.5) os.system('docker container rm '+dockername) time.sleep(0.5) os.system('docker run -d -p '+str(portnum)+':4444 --shm-size=2g --name '+dockername+' --dns 168.95.1.1 selenium/standalone-chrome:103.0') time.sleep(7) driver=init_webdriver(portnum) #driver.request_interceptor = interceptor driver.get('https://cmm.ai/101-aiv1') time.sleep(3) urls=[] click_btn("//button[contains(text(),'中文')]") time.sleep(3) click_btn("//button[@class='close-btn']") time.sleep(3) click_btn("//button[contains(text(),'點我問問題')]") time.sleep(3) i_key=random.choice(qs) elmts = driver.find_elements(By.XPATH, "//input[contains(@placeholder,'請輸入訊息')]") for elmt in elmts: elmt.send_keys(i_key) elmt.send_keys(Keys.ENTER) #中文 time.sleep(10) print('----done----')