簡體   English   中英

隨機點擊保持 session 活着 selenium python

[英]random click to keep session alive selenium python

在使用 selenium 時,如何在 n 秒后隨機單擊頁面上的某處以保持 session 處於活動狀態? 刮擦正在進行,如果我們不做任何事情,它將斷開 session。 有沒有其他方法可以做一些活動/保持 session 活着?

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()

為了使用 selenium 執行點擊,您可以執行以下兩個步驟。 當您的程序啟動時,創建一個線程或另一個在每n 秒后不斷單擊的進程。 您可以通過使用 python 線程模塊或多處理模塊輕松做到這一點。

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

盡管另一個建議是在使用 selenium 時使用headless

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM