如何使用Selenium和Python在网页的输入字段中传递经过验证的用户输入 [英] How to pass validated user input with in an input field of a webpage using Selenium and Python

查看:95
本文介绍了如何使用Selenium和Python在网页的输入字段中传递经过验证的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

从硒导入Webdriver的

 导入时间浏览器= webdriver.Chrome('C:/Users/acer/Desktop/chromedriver')browser.get('website')x = str(input('Your name:'))#用户在此处输入名称如果len(x)>20:#van的名称不得超过20个字符打印(较短")x#如果再次超过20个输入否则:#如果以上正确,请执行以下操作:def user():而True:time.sleep(1)尝试:browser.find_element_by_id('q').send_keys(x)#使用html id q查找输入字段并输入名称browser.find_element_by_tag_name('button').click()#click下一步用户() 

解决方案

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

  • 打开网址 https://www.google.com/
  • 接受用户输入,即 name
  • 验证 name 的长度是否小于5个字符.
    • 如果为true,请 打破 循环,然后将 name 传递给 Google主页搜索框.
    • 如果为假,请 继续 再次请求用户输入.
  • 代码块:

    从硒导入Webdriver的

     从selenium.webdriver.support.ui导入WebDriverWait来自selenium.webdriver.common.by导入方式从selenium.webdriver.support导入EC的预期条件从selenium.common.exceptions导入TimeoutException,WebDriverException选项= webdriver.ChromeOptions()options.add_argument(开始最大化")options.add_experimental_option("excludeSwitches",[启用自动化"])options.add_experimental_option('useAutomationExtension',False)浏览器= webdriver.Chrome(options = options,execute_path = r'C:\ WebDrivers \ chromedriver.exe')browser.get('https://www.google.com/')而True:名称= str(input(请输入名称(最多5个字符):"))如果len(name)>5:打印(超过5个字符,请重试...")继续别的:休息WebDriverWait(浏览器,20).until(EC.element_to_be_clickable((By.NAME,"q"))).send_keys(name) 

  • 控制台输出:

     请输入名称(最多5个字符):asdfgh超过5个字符,请重试...请命名(最多5个字符):asdfghjkl超过5个字符,请重试...请命名(最多5个字符):dev 


此用例

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

从硒导入Webdriver的

 选项= webdriver.ChromeOptions()options.add_argument(开始最大化")options.add_experimental_option("excludeSwitches",[启用自动化"])options.add_experimental_option('useAutomationExtension',False)浏览器= webdriver.Chrome(options = options,execute_path = r'C:\ WebDrivers \ chromedriver.exe')browser.get('https://www.google.com/')而True:名称= str(input(请输入名称(最多20个字符):"))如果len(name)>20:打印(超过20个字符,请重试...")继续别的:休息browser.find_element_by_id('q').send_keys(name)#使用html id q查找输入字段并输入名称 

I want the user to type in her/his name then pass the user input to selenium so that it automatically types it in the search field for my website. Is possible to first input, press enter then trigger selenium to forward the input to the browser. Why is it not working?

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

解决方案

To demonstrate passing the user input to the input field when the user's name is not longer than 20 charachters, here is a small program which does the following:

  • Opens the url https://www.google.com/
  • Takes user input i.e.name
  • Validates if the length of name is less then 5 characters.
    • if true, break the loop and pass the name to the Google Home Page search box.
    • if false, continue to ask for user input again.
  • Code Block:

    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)
    

  • Console Output:

    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
    


This usecase

You can follow similar logic for a 20 character long user input and your effective code block will be:

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

这篇关于如何使用Selenium和Python在网页的输入字段中传递经过验证的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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