如何将 url 作为用户输入传递以通过 Selenium 和 Python 进行调用? [英] How to pass urls as user input to invoke through Selenium and Python?

查看:26
本文介绍了如何将 url 作为用户输入传递以通过 Selenium 和 Python 进行调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行我的脚本时,我希望它使用 input() 询问我的目标 url 是什么,以便它可以将其存储为变量并在函数中使用它,但是每次我输入 url 时,脚本都不会继续并按 Enter 会导致它在我的默认浏览器上打开 url 作为选项卡而不是新的 chrome 对象.

Every time I run my script I want it to ask me what is the targeted url using input() so it can store it as a variable and use it in a function but every time I input the url the script won't continue and pressing enter causes it to open the url on my default browser as a tab instead of a new chrome object.

def function1(targeturl):
 driver = webdriver.Chrome()
 driver.get(targeturl)

print('What is the website?')
webPage = input()
function1(webPage)

我不确定 IDE 是否重要,但我正在使用 Pycharm.我会在它询问我后复制并粘贴 url,当我按 Enter 时,它将打开 url 而不是继续脚本

I'm not sure whether the IDE is important but I'm using Pycharm. I will copy and paste the url in after it asks me and when I press enter it will open the url instead of continuing the script

推荐答案

要调用作为用户 inputurl,您可以使用 input() 功能.

To invoke a url taken as user input you can use the input() function.

这是您自己的程序,其中包含一些简单的增强功能,它将从用户那里一个接一个地接受 3 个 url 并导航到相应的 url:

Here is your own program with some simple enhancements which will accept 3 urls one by one from the user and navigate to the respective urls:

  • 代码块:

  • Code Block:

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()

  • 控制台输出:

  • Console Output:

    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
    

  • 这篇关于如何将 url 作为用户输入传递以通过 Selenium 和 Python 进行调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆