每次运行如何使用f-strings更改所调用的项目

发布于 2025-01-22 07:51:59 字数 743 浏览 0 评论 0原文

因此,我正在尝试进行制作,以便每次运行for for for for for for

。工作:

from selenium import webdriver

driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()
driver3 = webdriver.Chrome()

drivers = ['driver1', 'driver2', 'driver3']

for i in range(1, 3):
    driver[f'{i}'].get('google.com')

获得typeError:

'int' object is not subscriptable

driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()
driver3 = webdriver.Chrome()

drivers = ['driver1', 'driver2', 'driver3']

for driver in drivers:
    [f'{drivers}'].get('google.com')

给我一个属性:

'list' object has no attribute 'get'

我觉得这很简单,但是无论我修改了我的Python基础知识,我都无法直接得到它。

So I'm trying to make so everytime the for loop runs, it changes slightly the driver.get() function, so i can shorten my code instead of typing driver1.get, driver2.get driver3.get

I tried to things and neither work:

from selenium import webdriver

driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()
driver3 = webdriver.Chrome()

drivers = ['driver1', 'driver2', 'driver3']

for i in range(1, 3):
    driver[f'{i}'].get('google.com')

Gets a TypeError:

'int' object is not subscriptable

and

driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()
driver3 = webdriver.Chrome()

drivers = ['driver1', 'driver2', 'driver3']

for driver in drivers:
    [f'{drivers}'].get('google.com')

gives me an AttributeError:

'list' object has no attribute 'get'

I feel like this is a simple matter, but I cant get it straight no matter how much I revise my Python Basics book.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷梦 2025-01-29 07:51:59
drivers=[]
for i in range(3):
    driver = webdriver.Chrome()
    drivers.append(driver)

两种方式:

for i in range(3):
    drivers[i].get('https://www.google.com/')

for driver in drivers:
    driver.get('https://www.google.com/')

您可以这样做以使X驱动器数量并访问它们。

drivers=[]
for i in range(3):
    driver = webdriver.Chrome()
    drivers.append(driver)

Two ways:

for i in range(3):
    drivers[i].get('https://www.google.com/')

for driver in drivers:
    driver.get('https://www.google.com/')

You could just do that to make x amount of drivers and access them.

谈情不如逗狗 2025-01-29 07:51:59

f-scrings> f-strings python中的是一种字符串格式的机制。

因此,在线中,

driver[f'{i}'].get('google.com')

您需要将 i 的类型转换为字符串,同时通过如下:

driver[f'{str(i)}'].get('google.com')

f-strings in Python is a string formatting mechanism.

So within the line,

driver[f'{i}'].get('google.com')

you need to convert the type of i as string while passing as follows:

driver[f'{str(i)}'].get('google.com')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文