Fórum Root.cz

Hlavní témata => Vývoj => Téma založeno: Wangarad 27. 01. 2021, 08:37:33

Název: Python alternativa k selenium
Přispěvatel: Wangarad 27. 01. 2021, 08:37:33
Zdravim.
Existuje alternativa k selenium alebo nieco podobne co by riesilo problem otvarania stranky a vyplnenia formularu?
Ide o to ze mam externe zariadenie s ktoreho sa nacita ID. Selenium nasledne otvara stranku vyplna vstupne data a prihlasuje do systemu. Cele to mam funkcne ale je to neskutocne "divne" na bezne pouzitie. Idealne nieco co vie detekovat uz otvoreny browser a vykonat akciu v nom. Skusal som priamo webbrowser ale tam sa mi nepodarilo vyriesit problem s vlozenim udajov na web. (Ten viac menej riesil moj probem s pouzitim aktivneho prehliadaca)
Kód: [Vybrat]
import serial
import re
import subprocess
import win32com.client
import time
from selenium import webdriver

#Open serial connection
ser = serial.Serial()
ser.port = "COM7"
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None          #block read
ser.timeout = 1            #non-block read
#ser.timeout = 2              #timeout block read
ser.xonxoff = False     #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False       #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2     #timeout for write
ser.open() # try to open port, if possible print message and proceed with 'while True:'
print ("port is opened!")
# Only trash

while (True):
    if ser.is_open:
        if (ser.inWaiting()>0):
            time.sleep(0.5) #if incoming bytes are waiting to be read from the serial input buffer
            data_str = ser.read(ser.inWaiting()).decode('ascii') #read the bytes and convert from binary array to ASCII
            print(data_str, end='\n') #print the incoming string without putting a new-line ('\n') automatically after every print()
            time.sleep(0.01)
            #Open Browser
            #browser exposes an executable file
            #Through Selenium test we will invoke the executable file which will then #invoke actual browser
            driver = webdriver.Chrome(executable_path="C:\driver\chromedriver.exe")
            # to maximize the browser window
            driver.maximize_window()
            #get method to launch the URL
            driver.get("http://XXXX/ads/idlogin.php?")
            #to refresh the browser
            driver.refresh()
            # identifying the edit box with the help of id and enter text
            driver.find_element_by_name("loginname").send_keys(data_str)
            # identifying the button then using click() method
            driver.find_element_by_xpath("//button[contains(@class,'submitbutton')]") .click()
Název: Re:Python alternativa k selenium
Přispěvatel: Roma_CZ 27. 01. 2021, 10:28:04
Krom Selenia můžeš vyzkoušet Pyppeteer - neoficiální Pythoní port Puppeteeru od Googlu
https://github.com/pyppeteer/pyppeteer (https://github.com/pyppeteer/pyppeteer)
Nebo Playwright - vznikl když MS přetáhl původní vývojáře Puppeteeru
https://playwright.dev/ (https://playwright.dev/)
Název: Re:Python alternativa k selenium
Přispěvatel: panpanika 27. 01. 2021, 17:05:42
urcite k tomu potrebujes celej browser? neda se ten formular poslat pomoci requests?
Název: Re:Python alternativa k selenium
Přispěvatel: Roma_CZ 28. 01. 2021, 13:43:00
Přišlo mi že chce něco předklikat v prohlížeči (idálně už v tom, co má otevřený) a dál pokračovat ručně.

Jinak Requests-HTML https://requests.readthedocs.io/projects/requests-html/en/latest/ (https://requests.readthedocs.io/projects/requests-html/en/latest/) vnitřně taky používá pyppeteer.