【发布时间】:2019-06-01 00:35:46
【问题描述】:
(不相关的背景信息:我的笔记本电脑被盗了,我正在做这个,所以如果它再次发生,我有很大的机会找回它)
问题:
所以我正在尝试运行一个 python 脚本,该脚本将(秘密)根据本网站截取当前地理位置: https://gps-coordinates.org/my-location.php
如果没有 headless 选项,它可以很好地工作,但有了它,位置就无法加载。我怀疑无头模式不允许正确弹出窗口或警报,因此 chrome 无法“记住”我始终允许定位的选择。
我查看了源代码,位置信息出现在“leaflet-popup-content”类下
我的尝试:
我已经尝试过使用等待元素存在
如下:
def waitForLoad(browser):
try:
WebDriverWait(browser, 10, ignored_exceptions=[
NoSuchElementException, TimeoutException
]).until(EC.presence_of_element_located((By.CLASS_NAME,'leaflet-popup-content')))
except TimeoutException:
print("Error, timed out, trying again")
browser.refresh()
waitForLoad(browser)
return
我也尝试过使用 firefox 驱动程序 (geckodriver),但在设置 headless 之前它甚至无法正常工作
代码:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-infobars")
browser=webdriver.Chrome('/home/tclack/bin/chromedriver',chrome_options=chrome_options)
# YMMV this is the chromedriver I use and the location
url = "https://gps-coordinates.org/my-location.php"
browser.get(url)
browser.save_screenshot('screenshot.png')
browser.close()
browser.quit()
预期的输出是一个 .png 文件,其中选择了当前位置,如果您运行它但注释掉“--headless”行(第 6 行),您会看到。 (还假设该网站在允许位置方面被“列入白名单”)
【问题讨论】: