python - 如何将 url 作为用户输入传递以通过 Selenium 和 Python 调用?
问题描述
每次我运行我的脚本时,我希望它使用 input() 询问我的目标 url 是什么,以便它可以将其存储为变量并在函数中使用它,但每次我输入 url 时,脚本都不会继续并按下enter 导致它在我的默认浏览器上将 url 作为选项卡而不是新的 chrome 对象打开。
def function1(targeturl):
driver = webdriver.Chrome()
driver.get(targeturl)
print('What is the website?')
webPage = input()
function1(webPage)
我不确定 IDE 是否重要,但我正在使用 Pycharm。在它询问我之后,我将复制并粘贴 url,当我按下回车键时,它将打开 url 而不是继续脚本
解决方案
要调用作为用户输入的url ,您可以使用该函数。input()
这是您自己的程序,带有一些简单的增强功能,它将从用户那里一一接受 3 个url并导航到相应的url:
代码块:
from selenium import webdriver def function1(targeturl): driver.get(targeturl) # perform your taks here driver = webdriver.Chrome() for i in range(3): webPage = input("What is the website url?(Press enter at the end to continue):") function1(webPage) driver.quit()
控制台输出:
What is the website url?(Press enter at the end to continue):http://www.google.com What is the website url?(Press enter at the end to continue):http://www.facebook.com What is the website url?(Press enter at the end to continue):http://www.gmail.com
推荐阅读
- reactjs - React PropType 用于同一类的一种或多种类型
- cmake - protoc v2.6.1 是否应该产生“预期的“必需”、“可选”或“重复”。
- rust - Rust/Diesel:如何查询并插入具有 uuid 的 postgres 表
- c# - 使用dot net core和c#在sql server中存储GTFS实时数据
- java - Java,重复x次字符串
- python - 循环;python中的结束语句
- ionic-framework - 在 Ionic v3 中,我突然收到此错误 cannot read _getPortal from undefined
- react-native - 不变违规:requireNativeComponent:在 UIManager 中找不到“RNSVGPath”
- docker - 为什么docker容器名称末尾有一个随机数?
- tcl - 如何使用 tk_optionMenu 过程?