随机点击以保持会话活力selenium python



如何在n秒后随机点击页面上的某个位置,以在使用硒的同时保持会话的活力?刮痧正在进行,如果我们不采取措施,就会中断会话。有没有其他方法可以做一些活动/保持会话的活力?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import json
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

options = Options()
options.add_argument("user-data-dir=C:\Users\user\AppData\Local\Google\Chrome\User Data")
driver = webdriver.Chrome(executable_path='C:/chromedriver.exe', options=options)
driver.maximize_window()
driver.get('URL')
time.sleep(15)
def find_button():
driver.switch_to.frame(driver.find_element_by_xpath("//*[@id="root"]/main/div[1]/div/div/section/div[1]/div/iframe"))
try:
button = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[2]/div/div/div/div[3]/button')))
if button:
button.click()
except TimeoutException as e:
pass
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#root > div.css-1dskt1c.ev4oou50 > main > div.css-fxcew.eqhy1wt0 > div.css-1o0ko6s.ejzudwb1 > div.css-wp7h99.ebewhpb0 > section.css-n56t2b.e1bk88wc2 > header > div.css-8hdyh3.esmanvo0 > svg'))).click()
while True:
info = driver.find_element_by_css_selector('#root > div.css-1dskt1c.ev4oou50 > main > div.css-fxcew.eqhy1wt0 > div.css-1o0ko6s.ejzudwb1 > div.css-wp7h99.ebewhpb0 > section.css-conr1v.e1bk88wc2 > div > div > div.css-1t6u4d1.ezcyhjn1 > div.css-r8tf41.eyg06i70')
result = info.text
result = result.replace('n', ' ')
time.sleep(5)
dictionary = {"1st 12": result[6:22], "2nd 12": result[30:46]}
json_dump = json.dumps(dictionary)
print(json_dump)
find_button()

要使用硒执行单击,可以执行以下两个步骤。当程序启动时,创建一个线程或另一个进程,在每n秒后继续单击。您可以通过使用python线程模块或多处理模块轻松地做到这一点。

element = driver.find_element_by_id("myelement")
element.click()

尽管另一个建议是在使用硒的同时使用无头

最新更新