如何使用Selenium和Python在网页的输入字段中传递经过验证的用户输入

阿卜杜拉·罗达(Abdalla Roda)

我希望用户输入她/他的名字,然后将用户输入传递给selenium,以便它在我的网站的搜索字段中自动输入。可以先输入,然后按Enter,然后触发硒,将输入转发到浏览器。为什么不起作用?

from selenium import webdriver
import time

browser = webdriver.Chrome('C:/Users/acer/Desktop/chromedriver')
browser.get('website')

x = str(input('Your name: ')) #user inputs name here

if len(x) > 20: #the name van not be longer than 20 charachters
    Print(’shorter’)
    x # if longer than 20 input again

else: #if above is correct do following:
    def user():
        while True:
            time.sleep(1)
            try:
                browser.find_element_by_id('q').send_keys(x) #find input field with html id q and input the name
                browser.find_element_by_tag_name('button').click() #click next
    user()
DebanjanB

为了演示在用户名不超过20个字符时将用户输入传递给输入字段,下面是一个小程序,该程序执行以下操作:

  • 打开网址https://www.google.com/
  • 接受用户输入,即name
  • 验证的长度name是否小于5个字符。
    • 如果为true,break则循环播放并将其传递nameGoogle主页搜索框。
    • 如果为假,则continue再次要求用户输入。
  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException, WebDriverException
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    browser = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    browser.get('https://www.google.com/')
    while True:
        name = str(input("Name please (max 5 charachters):"))
        if len(name) > 5:
            print("More than 5 charachters, please try again...")
            continue
        else:
            break
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys(name)
    
  • 控制台输出:

    Name please (max 5 charachters):asdfgh
    More than 5 charachters, please try again...
    Name please (max 5 charachters):asdfghjkl
    More than 5 charachters, please try again...
    Name please (max 5 charachters):dev
    

这个用例

对于20个字符长的用户输入,您可以遵循类似的逻辑,您的有效代码块将为:

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
browser = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
browser.get('https://www.google.com/')
while True:
    name = str(input("Name please (max 20 charachters):"))
    if len(name) > 20:
        print("More than 20 charachters, please try again...")
        continue
    else:
        break
browser.find_element_by_id('q').send_keys(name) #find input field with html id q and input the name

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用VBA在网页中输入用户名和密码

如何使用Python Selenium在输入字段中输入数值?

如何在网页中输入值并使用python单击复选框

使用try和except来验证用户在Python中的输入

如何从文本字段获取用户输入并将其显示在网页上?

如何使用Selenium和Python更改输入字段的值

将用户输入从HTML页面传递到Perl脚本并在网页中显示结果

如何使用Selenium和Python根据用户输入查找元素?

使用C#和Selenium在网页的代码镜像文本框中输入多行SQL文本

如何验证python中的用户输入?

如何在python中验证用户输入

如何将经过验证的用户输入传递给方法参数?

使用 Selenium 在网站中输入数据?

如何在 Selenium 中验证电子邮件用户输入?

如何将url作为用户输入传递以通过Selenium和Python调用?

如何使用Excel VBA在网页的下拉框中输入值

如何使用Selenium Webdriver在网页中基于用户ID选择操作?

在网页上检测用户输入(按键或输入的事件处理)

如何使用 try 和 catch 验证用户输入?

PyQt5和Python中的用户输入验证

无法使用 selenium 和 python 删除输入字段

PYTHON:xPath 不会定位和输入字段。使用 selenium、Webdriver 等

如何使用Selenium在Python中输入文本

使用@Pattern时如何获取经过验证的输入的值?

如何在方法调用中传递数据和使用用户输入

我如何在 python 中验证用户输入?

Python如何使用多个可接受的选项验证用户输入

在python中使用selenium在输入标签中填写用户名和密码

如何使用 Selenium 和 Python 3.6 在网页上定位元素