02
2020
12

selenium aoto-fill self declaration form

#!/usr/bin/python3

#!/usr/bin/env python3



from pyvirtualdisplay import Display

from selenium import webdriver

import time


display = Display(visible=0, size=(800, 600))

display.start()


m=str(time.strftime("%m", time.localtime()))

d=str(time.strftime("%d", time.localtime()))



browser = webdriver.Firefox()

browser.get('https://docs.google.com/forms/d/e/1FAIpQLSe_o66UFMh00c741fLlzGnEnxSGXRV5ioOFFpfxUeK5PmbkbA/viewform')


#print(browser.page_source)

#title = browser.title

#print(title.text)


id=browser.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input")

id.send_keys("06836")

time.sleep(1)


name=browser.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input")

name.send_keys("Ng Yik Yiu")

time.sleep(1)


mth=browser.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div/div[1]/div/div[2]/div[1]/div/div[1]/input")

mth.send_keys(m)

date=browser.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div/div[3]/div/div[2]/div[1]/div/div[1]/input")

date.send_keys(d)

time.sleep(1)


ck=browser.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div/span/div/div[1]/label/div/div[1]/div/div[3]/div")

ck.click()

time.sleep(1)


browser.get_screenshot_as_file("NYO"+"-"+m+"-"+d+".png")


nx=browser.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[3]/div[1]/div/div/span")

nx.click()

time.sleep(3)


submit=browser.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[3]/div[1]/div/div[2]/span/span")

submit.click()

time.sleep(1)


browser.get_screenshot_as_file("NYO_finished"+"-"+m+"-"+d+".png")


print("Declaration form is submitted!")

browser.close()

display.stop()












google colab example


#install the following modules first

#!pip install selenium

#!apt-get update 

#!apt install chromium-chromedriver


# coding=utf-8

import os

import time

from google.colab import drive

from google.colab import files

import gspread

import pandas as pd

from google.colab import auth

auth.authenticate_user()

from oauth2client.client import GoogleCredentials

from selenium import webdriver





drive.mount('/content/drive', force_remount=False)

path = "/content/drive/My Drive"

os.chdir(path)

os.listdir(path)


m=str(time.strftime("%m", time.localtime()))

d=str(time.strftime("%d", time.localtime())) 



chrome_options = webdriver.ChromeOptions()

chrome_options.add_argument('--headless')

chrome_options.add_argument('--no-sandbox')

chrome_options.add_argument('--disable-dev-shm-usage')

wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)


n=["Ng Yik Yiu","Tam Ka Yu","Chan Tak Lam, Terence","Lai Shuk Ming, Shirley","Huynh Kwok Ying","Chan Ching Lam, Fiona","Chu Chung Him"]

s=["06836","07252","05413","07724","07063","06994","06952"]


for na, no in zip(n, s):

    wd.get("https://docs.google.com/forms/d/e/1FAIpQLSe_o66UFMh00c741fLlzGnEnxSGXRV5ioOFFpfxUeK5PmbkbA/viewform")

    title = wd.title

    print((na)+"'s Self Declaration Form")


    id=wd.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input")

    id.send_keys(no)

    time.sleep(1)


    name=wd.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input")

    name.send_keys(na)

    time.sleep(1)




    mth=wd.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div/div[1]/div/div[2]/div[1]/div/div[1]/input")

    mth.send_keys(m)

    date=wd.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div/div[3]/div/div[2]/div[1]/div/div[1]/input")

    date.send_keys(d)


    js="var q=document.documentElement.scrollTop=350"

    wd.execute_script(js)

    wd.get_screenshot_as_file("/content/drive/My Drive/"+(na)+"-N-"+m+"-"+d+".png")



    ck=wd.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div/span/div/div[1]/label/div/div[1]/div/div[3]/div")

    ck.click()

    time.sleep(1)


    js="var q=document.documentElement.scrollTop=900"

    wd.execute_script(js)

    #wd.get_screenshot_as_file("/content/drive/My Drive/"+(na)+"-"+m+"-"+d+".png")



    nx=wd.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[3]/div[1]/div/div/span")

    nx.click()

    time.sleep(3)




    submit=wd.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/div/div[3]/div[1]/div/div[2]/span/span")

    #submit.click()

    #time.sleep(1)


    #if you need to extract text of the element, please just use .text     e.g. print(submit.text)


    wd.get_screenshot_as_file("/content/drive/My Drive/"+(na)+"-F-"+m+"-"+d+".png")

wd.quit()
















Locating Elements

There are various strategies to locate elements in a page. You can use the most appropriate one for your case. Selenium provides the following methods to locate elements in a page:

  • find_element_by_id

  • find_element_by_name

  • find_element_by_xpath

  • find_element_by_link_text

  • find_element_by_partial_link_text

  • find_element_by_tag_name

  • find_element_by_class_name

  • find_element_by_css_selector

To find multiple elements (these methods will return a list):

  • find_elements_by_name

  • find_elements_by_xpath

  • find_elements_by_link_text

  • find_elements_by_partial_link_text

  • find_elements_by_tag_name

  • find_elements_by_class_name

  • find_elements_by_css_selector


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。