-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgeo.py
69 lines (58 loc) · 2.57 KB
/
geo.py
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
64
65
66
67
68
#!/usr/bin/env python3
# Copyright (c) 2020 Serguei Kouzmine
#
# used to answer the quesion https://software-testing.ru/forum/index.php?/topic/38904-kak-zadat-geolokatciiu-dlia-okna-chromedriver-selenium-python/ (in Russian)
from __future__ import print_function
import time
import sys
from os import getenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions
import json, base64
def send_command(driver, cmd, params = {}):
post_url = driver.command_executor._url + '/session/{0:s}/chromium/send_command_and_get_result'.format( driver.session_id)
print ('POST to {}'.format(post_url))
print('params: {}'.format(json.dumps({'cmd': cmd, 'params': params})))
# see also: https://github.com/SeleniumHQ/selenium/blob/cdp_codegen/dotnet/src/webdriver/Chromium/ChromiumDriver.cs#L69
response = driver.command_executor._request('POST', post_url, json.dumps({'cmd': cmd, 'params': params}))
if ('status' in response ) and response['status']:
raise Exception(response.get('value'))
if __name__ == '__main__':
if getenv('OS') != None :
homedir = getenv('USERPROFILE').replace('\\', '/')
else:
homedir = getenv('HOME')
options = Options()
driver = webdriver.Chrome( homedir + '/' + 'Downloads' + '/' + 'chromedriver', options = options)
latitude = 37.422290
longitude = -122.084057
latitude = 55.7039
longitude = 37.5287
params = {
'latitude': latitude,
'longitude': longitude,
'accuracy': 100,
}
url = 'https://www.google.com/maps'
print('Loading url: "{}"'.format(url), file = sys.stderr)
driver.get(url)
WebDriverWait(driver, 120).until( expected_conditions.presence_of_element_located((By.ID,'widget-mylocation')))
# NOTE: frequent cast error:
# TypeError: __init__() takes 2 positional arguments but 3 were given
# time.sleep(10)
print('params: {}'.format(params))
# https://chromedevtools.github.io/devtools-protocol/tot/Emulation#method-setGeolocationOverride
send_command(driver, 'Emulation.setGeolocationOverride', params)
try:
element = WebDriverWait(driver, 120).until( expected_conditions.visibility_of(driver.find_element_by_css_selector('div[class *= "widget-mylocation-button-icon-common"]')))
# NOTE: this wait methos appears less reliable then xxx_located method
if element != None:
element.click()
except TimeoutException:
pass
time.sleep(10)
driver.quit()