问题
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 thename
to the Google Home Page search box. - if false,
continue
to ask for user input again.
- if true,
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