小编Mar*_*son的帖子

如何从 python selenium firefox webdriver 获取 geckodriver 版本?

我正在尝试编写一些防御代码,以防止有人安装旧版本的 geckodriver 时执行脚本。我似乎无法从 webdriver 对象中获取 geckodriver 版本。

我发现的最接近的是driver.capabilities包含 firefox 浏览器版本,但不包含 geckodriver 版本。

from selenium import webdriver
driver = webdriver.Firefox()
pprint(driver.capabilities)
Run Code Online (Sandbox Code Playgroud)

输出:

{'acceptInsecureCerts': True,
 'browserName': 'firefox',
 'browserVersion': '60.0',
 'moz:accessibilityChecks': False,
 'moz:headless': False,
 'moz:processID': 18584,
 'moz:profile': '/var/folders/qz/0dsxssjd1133p_y44qbdszn00000gp/T/rust_mozprofile.GsKFWZ9kFgMT',
 'moz:useNonSpecCompliantPointerOrigin': False,
 'moz:webdriverClick': True,
 'pageLoadStrategy': 'normal',
 'platformName': 'darwin',
 'platformVersion': '17.5.0',
 'rotatable': False,
 'timeouts': {'implicit': 0, 'pageLoad': 300000, 'script': 30000}}
Run Code Online (Sandbox Code Playgroud)

浏览器版本和 geckodriver 版本是否可以直接链接?如果没有,我如何从 python 中检查 geckodriver 版本?

python selenium selenium-firefoxdriver geckodriver

4
推荐指数
1
解决办法
1万
查看次数

是否可以在列表理解中创建和引用对象?

我有一个网址列表,我想要净位置.

urls = ["http://server1:53000/cgi-bin/mapserv?map=../maps/Weather.wms.map", 
        "http://server2:53000/cgi-bin/mapserv?map=../maps/Weather.wms.map"]
Run Code Online (Sandbox Code Playgroud)

我通常会写这样的东西:

servers = []
for url in urls:
    o = urlparse(url)
    servers.append(o.netloc)
Run Code Online (Sandbox Code Playgroud)

然后我立刻想到,"我应该把它理解为"并继续写这个(当然这不起作用):

servers = [o.netloc() for urlparse(url) as o in urls]
Run Code Online (Sandbox Code Playgroud)

python有办法做这种复杂的理解吗?(也许在3.x?)

在更具学术性的层面上,这种复杂的理解能否与"pythonic"相距甚远?这对我来说似乎相对直观,但我之前完全偏离了这些事情.

python list-comprehension python-2.7

3
推荐指数
1
解决办法
371
查看次数

ruff :匹配/大小写语法上的 E999 SyntaxError

当我使用匹配/大小写语法时,我遇到了 ruff (0.0.209) 和 python 3.10.9 解释器的奇怪问题。例如,这个简单的代码:

from http import HTTPStatus

http_status = HTTPStatus.OK

match http_status:
    case HTTPStatus.OK:
        print("OK!")

    case HTTPStatus.BAD_REQUEST:
        print("Bad, bad Zoot!")

    case _:
        print("Just a flesh wound.")
Run Code Online (Sandbox Code Playgroud)

提出一个5:8: E999 SyntaxError: invalid syntax. Got unexpected token 'http_status'

同时,ruff 辩称它现在支持 python3.11。谁在说谎?:)

我尝试用最新版本更新 ruff

python-3.x linter python-3.10 ruff

2
推荐指数
1
解决办法
1721
查看次数

有没有办法避免这个重复的代码?

我编写了一段相当简单的代码,但仍然有这种唠叨的感觉,有一种更简单的方法来编写它.特别是两个省; 我觉得必须有一种方法可以避免在这个循环中编写两个相同的else子句.我是否密集?

parentid = ca.get_parent_id(spacename, parentname)
if parentid is None:
    raise Exception("Unable to find parent page")

for subpage in subpages:
    ## check to see if subpage is a child of the previous page
    childreninfo = ca.get_page_children(parentid)
    if childreninfo['children']['page']['results']:
        for pageinfo in childreninfo['children']['page']['results']:
            if pageinfo['title'] == subpage:
                break
        else:
            # if we find the page somewhere in the space but it's not a child, stop! 
            info = ca.get_page_info_by_title(subpage, spacename)
            if len(info['results']) == 1:
                raise Exception("Found page but not in right location") …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

1
推荐指数
1
解决办法
69
查看次数

python selenium perfLoggingPrefs 过滤

根据chromedriver 文档,我应该能够在记录性能时关闭来自 Page 域的事件。我试过设置 perfLoggingPrefs 但我仍然收到 Page 事件。我正确设置了吗?

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = webdriver.ChromeOptions()
options.add_argument("--disable-extensions")
options.add_argument("--allow-running-insecure-content")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--disable-single-click-autofill")
options.add_argument("--disable-autofill-keyboard-accessory-view[8]")
options.add_argument("--disable-full-form-autofill-ios")

options.headless = True
options.add_argument('--disable-gpu')

caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {
    'browser': 'ALL',
    'performance' : 'ALL',
    }
caps['perfLoggingPrefs'] = {
    'enableNetwork' : True,
    'enablePage' : False,
    'enableTimeline' : False
    }

driver = webdriver.Chrome(options=options, desired_capabilities=caps)

###  connect to my site, do some actions then I call
perfs = driver.get_log('performance')
for row in perfs:
    print(perfs)
Run Code Online (Sandbox Code Playgroud)

输出: …

python selenium selenium-chromedriver

1
推荐指数
1
解决办法
1395
查看次数