08
2020
11

teaching materials

selenium

https://www.techbeamers.com/locate-elements-selenium-python/#locate-element-by-name


http://www.testclass.net/selenium_python





selenium - 我们如何在colab.research.google.com中使用Selenium Webdriver?

原文 标签 selenium selenium-webdriver google-colaboratory


我想在colab.research.google.com中使用Chrome的Selenium Webdriver进行快速处理。我可以使用!pip install selenium安装Selenium,但chrome的webdriver需要通向webdriverChrome.exe的路径。我应该如何使用它?


附注-colab.research.google.com是一个在线平台,可为与深度学习相关的快速计算问题提供GPU。请避免使用诸如webdriver.Chrome(path)之类的解决方案。



您可以通过安装Chrome Web驱动程序并调整一些选项来做到这一点,以使其在Google colab中不会崩溃:



!pip install selenium

!apt-get update # to update ubuntu to correctly run apt install

!apt install chromium-chromedriver

!cp /usr/lib/chromium-browser/chromedriver /usr/bin

import sys

sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')

from selenium import webdriver

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)

wd.get("https://www.google.com")









info_list=[]


name, worker_id, card_id = input("").split()


info_list.append({"Name": name, "Staff ID": worker_id, "ID": card_id})

print("【info】:Added successfully")

print(info_list[0])


print("\n")


for i in info_list:

    for c, b in i.items():

    print("%s:%s" %(c, b))






column = ['StaffID ','name ','gender ','birthday']


row = [['10001','Ho Yin','男','1986-0-0'],['10002','YoYo','女','2017-0-0'],['10003','TTL','男','1990-0-80']]



print("\n\n")


#for k in range(len(row)):

#print(k)

print("\n\n")


for k in range(len(row)):

rowtxt = '{}|{}|{}|{}'.format(row[k][0],row[k][1],row[k][2],row[k][3])

print(rowtxt)

print("\n\n")

for j in range(len(row[k])):

print(row[k][j])

a=row[k][j]

row.append(a)

print("\n\n")


print(row)








 



practice for append command


li= [{"a": 5}, {"b": 9}, {"c": -1}, {"d": 4}]


li.append({"e":123})


#print(li)

for i in range (len(li)):

print(li[i])





f=open("data.txt","w")

for i in info_list:

    for c, b in i.items():

    f.write(str(c)+':'+str(b)+'\n')

f.close()









sd = {}

f = open('data.txt','r')

for line in f.readlines():

    line = line.strip()#remove head and tail space

    c = line.split(':')[0]#separate with : and be the lefthand side first element

    b = line.split(':')[1]

    sd[c] = b

f.close()

print("read fromdata.txt: ",sd)














a = {'a' : 1, 'b': 2, 'c' : 3}


k = list(a.keys())


print('key:', k)


v = list(a.values())


print('value:', v)


print("\n\n")


#字典转换为列表

import collections


z = collections.OrderedDict()

z['b'] = 2

z['a'] = 1

z['c'] = 3

z['r'] = 5

z['j'] = 4


print(z)


#字典中的key转换为列表

k = list(z.keys())


print('字典中的key转换为列表:', k)


#字典中的value转换为列表

v = list(z.values())

print('字典中的value转换为列表:', v)






dict save to txt


dict = {'姓名':'TTL','性别':'女'}

 

filename = open('data.txt','w')#dict转txt

for k,v in dict.items():

    filename.write(k+':'+str(v))

    filename.write('\n')

filename.close()










Listing practice


list1 = ['SuperMan', 'BatMan', 'The Mask', 'SpiderMan'];


#Print entire list

print ("The list is: \n", list1)


#Reverse the list

list1.reverse()


#Print entire list

print ("\nThe reversed list is: \n", list1)


#Print the index of an object within the list

print ("\nThe object 'The Mask' is at index: ", list1.index('The Mask'))


#Remove object from the list

list1.remove('SpiderMan')


a=input("enter a string")


#Adding object to the list

list1.append(a)


#Print entire list

print ("\nAfter Removing SpiderMan: \n", list1)


print ("\n")

for i in range(len(list1)):

print(list1[i])

#for i in list1:

#print(list1)









loop a dict


li = [{"a": 5}, {"b": 9}, {"c": -1}, {"d": 4}]


for i in range (len(li)):

#print(li[i])

for j in li[i]:

print(j)






while loop


while True:

    try:

        grade = int(input("please input a number! \n")) #enter string will lead to error, if you wanna get rid of error, please use try.... exept ValueError

    except ValueError:

        print("plx input a number!")

        continue


    if grade == 60:

        print("Bingo")

        break


    elif grade <= 60 :

        print("it's less than that of the pre-set no.")

    

    elif grade >= 60:

    print("it's more than that of the pre-set no.")


    else:

        print something.....










list to dic


#list to dict

list1 = ['key1','key2']           

list2 = ['value1','value2']

a=zip(list1,list2)

d=dict(a)

print(d)


for i in d:

    print(i.values())







#!/usr/bin/python

# -*- coding: UTF-8 -*-


aList = [123, 'xyz', 'runoob', 'abc']


print ("xyz 索引位置: ", aList.index( 'xyz' ))

print ("runoob 索引位置 : ", aList.index( 'runoob')


xyz 索引位置:  1

runoob 索引位置 :  2












read from txt file

#import collections

#sd = collections.OrderedDict()

#f = open('data.txt','r')

#for line in f.readlines():

#    line = line.strip()

#    c = line.split(':')[0]

#    b = line.split(':')[1]

#    sd[c] = b

#f.close()

#print("read from data.txt: ",sd)



#python读取TXT每行,并存到LIST中

import sys

result=[]

with open('data.txt','r') as f:

for line in f:

result.append(list(line.strip('\n').split(':')))

print(result)








using pop

list1 = ['Google', 'Runoob', 'Taobao']


#consequently read the last element until the list becomes empty

try:

    while len(list1)>=0:

    b=list1.pop()

    print(b)

except IndexError:

print("error, empty list now!")










get multi-link


def getlink():

    page_links_list=[]

    pages=input("separate with space without comma").split()

    for page in pages:

        url="https://tw.stock.yahoo.com/q/q?s="+str(page)

        page_links_list.append(url)

    

    return page_links_list


#print(getlink())


for i in getlink():

print(i)




or set variables as following:



def getlink(j, i):

    page_links_list=[] 

    for page in range(int(j), int(i)+1):

        url="https://tw.stock.yahoo.com/q/q?s="+"{}".format(page)  #or str(i)

        page_links_list.append(url)

    return page_links_list


j=int(input("initiated page \n"))

i=int(input("final page \n"))


print("getlink list data")

print(getlink(j, i))













split, strip, join, replace



a="hello, i, am, kerek"

x=a.split(", ")

print(x)


for i in x:

print(i)

str = "00000003210Runoob01230000000"; 

#cut head and tail

print (str.strip("000000032"))




f = open('data.txt','r')

for line in f.readlines():

    line = line.strip()#remove head and tail space

    c = line.split(':')[0]#separate with : and be the lefthand side first element

    b = line.split(':')[1]

#    sd[c] = b

f.close()



print("\n")


print("split, strip, join, replace")


print("\n")



s1 = "="

seq = ("r", "u", "n", "o", "o", "b") # 字符串序列

print (s1.join( seq ))


#or

print("".join(seq))




print("\n")



#replace s with #

str = "this is string example....wow!!! this is really string";

print (str.replace("s", "#"))





adding items to list


list=[0, 1, 2, 3, 4, 5, 6]


list.insert(index no., 555)


print(list)


e.g. list.insert(3, 555)  =====> print(list)=====> result:  [0, 1, 2, 555, 3, 4, 5, 6]











import requests import lxml from bs4 import BeautifulSoup url="https://imgbin.com/png/0mCTHq7k/coronavirus-covid-19-png" response = requests.get(url) soup = BeautifulSoup(response.text, "lxml") images = soup.find_all('img') links=[] for i in images:    i.get('src')    #print(i.get('src'))    if ".jpg" and "cdn" in i.get('src'):        links.append(i.get('src')) print(links)


[links.append(i.get('src')) for i in images if ".jpg" and "cdn" in i.get('src')]print(links)
















#   ''' Returns the indexes of all occurrences of give element in the list'''


def get_indexes(list, element):

    index_pos_list = [i for i in range(len(list)) if list[i] == element or element in list[i]]

    return index_pos_list


#replace the indexes with your desired list of items

def replace(index_list,replacelist,list_tbm):

    for (index, replacement) in zip(index_list, replacelist):

        list_tbm[index] = replacement

    return list_tbm

    

replacements = [1,2,3] #your desired list of items   (replace list)


list = ['Hello', 'Ok', 'is', 'Ok', 'test', 'this', 'is', 'a', 'test', 'Ok'] #list to be modified


# Get indexes of all occurrences of 'Ok' in the list

index_pos_list = get_indexes(list, 'Ok')#indexes list

print('Indexes of all "Ok" in the list: ', index_pos_list)


print(replace(index_pos_list,replacements,list))












import re


data_list = ['cat_1', 'dog_1', 'dog_2', 'cat_2', 'dog_3', 'cat_3']  # 原列表中既有dog也有cat, 并且无规律


new_data_list = [data for data in data_list if  re.match('.*_2', data) != None]  # 保存筛选出来dog的列表


#for data in data_list:  # 遍历列表

#    if  re.match('.*_', data) != None:  # 如果正则匹配出的数据不为None, 就将此数据添加到新列表中

#        new_data_list.append(data)



#for data in data_list:  # 遍历列表

#    if  re.match('c.*', data) or re.match('d.*', data) != None:  # 如果正则匹配出的数据不为None, 就将此数据添加到新列表中

#        new_data_list.append(data)



#print('loop',new_data_list)



import re

# 筛选出data_list中所有的dog并保存在new_data_list中


data_list = ['cat_1', 'dog_1', 'dog_2', 'cat_2', 'dog_3', 'cat_3']  # 原列表中既有dog也有cat, 并且无规律


new_data_list = list(filter(lambda x: re.match('.*1', x) or re.match('.*2', x) != None, data_list))  # 生成新列表


print(new_data_list)
















 = ['1', '2', '3']

b = list(map(int, a))

print(b)


c = [print(i, type(i)) for i in b]


li = [1, 2, 3, 4, 5]

# 序列中的每个元素加1

print(list(map(lambda x: x+1, li))) # [2,3,4,5,6]


# 返回序列中的偶数

print(list(filter(lambda x: x % 2 == 0, li))) # [2, 4]



lt=['th-6 .tsDownload completed','th-6 h6KXZk5750653.jpgDownload completed','th-6 h6KXZk5750654.jpgDownload completed','th-6 h6KXZk5750655.pngDownload completed','   th-6 h6KXZk5750656.bpmDownload completed  ','th-6 h6KXZk5750657.bpmDownload completed   ','th-6 h6KXZk5750658.txtDownload completed','th-6 h6KXZk5750659.rmvbDownload completed','th-6 h6KXZk5750660.pyDownload']


f=list(filter(lambda x: '.bpm' in x, lt))


g=list(map(lambda x: x.strip().replace('completed','$]'), f))


print(f)

print(g)










l1 = ['b','c','d','b','c','a','a']


def duplicates(list, item):

return [i for i, x in enumerate(list) if x == item]

#print(duplicates(l1, "c"))




list2 = [1,2,3,2,1,5,6,5,5,5]


import collections

dup=[item for item, count in collections.Counter(list2).items() if count > 1]


#print('duplicated values: '+str(dup))

## [1, 2, 5]


#for i in dup:

#    print(duplicates(list2,i)) # return duplicated indexes

    

land=[duplicates(list2,i) for i in dup]


#print(land)




list2 = [1,2,3,2,1,5,6,5,5,5]


def duplicates(list, item):

return [i for i, x in enumerate(list) if x == item]



import collections

dup=[item for item, count in collections.Counter(list2).items() if count > 1]


print('duplicated values: '+str(dup))


DupIndexes=[duplicates(list2,i) for i in dup]


print(DupIndexes)







#filter with the same len of list


list = ["ark", "disembark", "quack", "rock", "pork"]

a=[x if "ck"in x else "" for x in list]

print(a)

#['', '', 'quack', 'rock', '']



======================================================================


List=["1a","2b","3c","4d","5e"]

for i in List:

  print(i)



print("\n\n")

List1 = [i  for i in List if "a" in i]

print(List1)


print("\n\n")


print("length "+str(len(List)))


print("\n\n")

for i in range(1, len(List)):

  print(List[i])


print("\n\n")

for i in range(1,10):

  print(i)


print("\n\n")

L=[i for i in range(1,10)]

print(L)


=======================================================================

import time

from google.colab import output


def run():

  greeting = "<<Hi! Welcome to Python~>>"

  while True:

    greeting= greeting[1:] + greeting[0]

    print(greeting)

    time.sleep(0.5)

    output.clear()


    

run()



========================================================================

# Python code to illustrate
# reduce() with lambda()

# to get sum of a list


from functools import reduce
list = [5, 8, 10, 20, 50, 100]
sum = reduce((lambda x, y: x + y), list)

print(sum)    


#(((((5+8)+10)+20)+50)+100)



e.g.


list = ["t""e""s""t"]
concat = reduce((lambda x, y: x + y), list)

print(concat)

#test


========================================================================


# a list contains both even and odd numbers. 
seq = [0, 1, 2, 3, 5, 8, 13]

  

# result contains odd numbers of the list
result1 = filter(lambda x: x % 2 != 0, seq)

print(list(result1))



max = lambda a, b : a if(a > b) else b print(max(1, 2)) print(max(10, 2))


lt=["i","c","h","i","2"] max = lambda a : a if "i" in a else "Nil" print(list(map(max,lt)))


  

# result contains even numbers of the list
result2 = filter(lambda x: x % 2 == 0, seq)
print(list(result2))


#output

result1 = [1, 3, 5, 13]
result2 = [0, 2, 8]

========================================================================


no_list = [1, 2, 3, 4, 5]

list(map(lambda n: n * 2, no_list)) 

#[2, 4, 6, 8, 10]













========================================

How To Install and Secure phpMyAdmin on Ubuntu 22.04

https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-22-04

« 上一篇 下一篇 »

发表评论:

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