gpt4 book ai didi

python - Selenium 函数中的引用变量

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:48 25 4
gpt4 key购买 nike

我想在循环中使用 selenium 和 python 向下滚动页面。

browser.execute_script("window.scrollTo(0, 40)")

^ 这到目前为止有效。

但是我怎样才能引用一个会随着每次迭代而增加的变量呢?

例如

def scroll():
global xx
xx = 10
while True:
browser.execute_script("window.scrollTo(0, xx)")
xx += 10`

我可以看到问题.. 输入 (window.scrollTo(0, xx)) 是一个字符串。

但是我不知道怎么解决。我需要更改执行的脚本吗?

最佳答案

只需使用字符串插值选项之一,例如:

def scroll():
xx = 10
while True:
browser.execute_script("window.scrollTo(0, {})".format(xx))
xx += 10

关于python - Selenium 函数中的引用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com