I am writing a web page panel of selenium crawler, using flask to control some behavior of selenium. The target web page needs to scan the QR code to log in, and now the card is at this stage after scanning the QR code, because I can"t figure out how to poll the status after login and return to Flask to trigger changes in the elements of the page. The specific code is as follows:
from selenium import webdriver
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.common.exceptions import TimeoutException,StaleElementReferenceException
class img_url_match(object):
def __init__(self,locator,text_):
self.locator = locator
self.text = text_
def __call__(self, driver):
try:
img_src = EC._find_element(driver,self.locator).get_attribute("src")
return img_src.startswith(self.text)
except StaleElementReferenceException:
return False
class control:
def __init__(self):
self.driver = webdriver.Chrome()
def qr(self):
self.driver.get("http://example.com")
try:
while True :
if not WebDriverWait(self.driver,2).until(img_url_match((By.ID,"js_login_qrcode_img"),"data:image/gif;base64")):
continue
else:
qr_src = self.driver.find_element_by_id("js_login_qrcode_img").get_attribute("src")
return qr_src
except TimeoutException:
self.driver.refresh()
def ck_login(self):
if self.driver.current_url == "http://example.com":
try:
url = WebDriverWait(self.driver,25).until(EC.url_changes("http://example.com"))
return "data:image/gif;base64," +self.driver.get_screenshot_as_base64()
except TimeoutException:
return False
elif self.driver.current_url == "http://example.com/home/":
return "data:image/gif;base64," + self.driver.get_screenshot_as_base64()
elif self.driver.current_url == ":data:,":
print("Now the url is :" + self.driver.current_url)
WebDriverWait(self.driver,5).until(EC.presence_of_element_located((By.ID,"js-ch-member-face")))
return "data:image/gif;base64," + self.driver.get_screenshot_as_base64()
if __name__ == "__main__":
test = control()
print(test.qr())
now I"m going to use Synergetic or subprocess to poll some states of selenium, but I wonder if this will launch another new selenium instance, that is, a new browser? Besides, wouldn"t it be better to use tornado?
Thank you.