-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnational.py
More file actions
63 lines (52 loc) · 1.94 KB
/
Copy pathnational.py
File metadata and controls
63 lines (52 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from selenium import webdriver
from selenium.common.exceptions import *
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.chrome.options import Options
from time import sleep
from openpyxl import Workbook
options = Options()
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.130 Safari/537.3")
options.add_experimental_option("debuggerAddress", "127.0.0.1:9030")
service = Service(executable_path = "C:\chromedriver-win64\chromedriver.exe")
driver = webdriver.Chrome(options = options)
def Find_Element(driver : webdriver.Chrome, by, value : str) -> WebElement:
while True:
try:
element = driver.find_element(by, value)
break
except:
pass
sleep(0.1)
return element
def Find_Elements(driver : webdriver.Chrome, by, value : str) -> list[WebElement]:
while True:
try:
elements = driver.find_elements(by, value)
if len(elements) > 0:
break
except:
pass
sleep(0.1)
return elements
def Send_Keys(element : WebElement, content : str):
element.clear()
for i in content:
element.send_keys(i)
sleep(0.1)
# driver.get('https://icp.administracionelectronica.gob.es/icpplus/index.html')
wb = Workbook()
sheet = wb.active
item = ['No', 'Nationality']
for i in range(0, 2):
sheet.cell(row = 1, column = i + 1).value = item[i]
nationalitys = Find_Elements(driver, By.TAG_NAME, 'option')
print(len(nationalitys))
start_row = 2
for nationality in nationalitys:
sheet.cell(row = start_row, column = 1).value = start_row - 1
sheet.cell(row = start_row, column = 2).value = nationality.text
start_row += 1
wb.save('nationality.xlsx')