每次运行如何使用f-strings更改所调用的项目
因此,我正在尝试进行制作,以便每次运行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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两种方式:
您可以这样做以使X驱动器数量并访问它们。
Two ways:
You could just do that to make x amount of drivers and access them.
f-scrings> f-strings python中的是一种字符串格式的机制。
因此,在线中,
您需要将
i
的类型转换为字符串,同时通过如下:f-strings in Python is a string formatting mechanism.
So within the line,
you need to convert the type of
i
as string while passing as follows: