How to pass validated user input with in an input field of a webpage using Selenium and Python

北慕城南 提交于 2021-01-27 18:12:02

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/59812328/how-to-pass-validated-user-input-with-in-an-input-field-of-a-webpage-using-selen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!