Home > Net >  Enter a value into a textbox using python and selenium
Enter a value into a textbox using python and selenium

Time:12-21

I am having some issues inputting a value into a textbox using selenium and python. I have tried out quite a few methods but haven't been able to make much progress.

This is what the websites HTML looks like (specifically the textbox I am looking to input a value into)

<input type="text"  name="location" id="location"  placeholder="Zip Code or City, State (e.g, 12345 or New York, NY)">&nbsp;&nbsp;<i ></i>

I am trying to enter the value 11415 for now, and have done this

 WebDriverWait(driver, 20).until(EC.element_to_be_clickable(By.XPATH, "//input[@class='form-control' and @id='location']")).send_keys("11415")

Here is the website if you'd like to check it out: https://www.fairhealthconsumer.org/medical/zip

And here is my full code for reference (the commented out code is other attempts I've made to solve this!)

import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time
import os
from selenium.webdriver.common.action_chains import ActionChains

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.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys



s=Service('/Users/[name]/Desktop/chromedriver')

chromeOptions = Options()
chromeOptions.headless = False
driver = webdriver.Chrome(service=s, options=chromeOptions)

list_data = []
    
def initalize_browser():
    # load website
    driver.get("https://www.fairhealthconsumer.org/")
    driver.maximize_window()

    driver.find_element(By.XPATH, '//button[@]').click()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, ".//*[@for='radio3']/span"))).click()
    driver.find_element(By.XPATH, ".//*[@for='radio3']//span[@class='pt-2 border-top border-white']").click()
    
    # inputElement = driver.find_element_by_id("location")
    # inputElement.send_keys('11415')
    
    # inputElement.send_keys(Keys.ENTER)

    driver.find_element(By.XPATH, '//button[@]').click()

    # driver.find_element(By.XPATH, '//*[@id="location"]').click()
    # driver.find_element(By.XPATH,'//text[@id="location"]').send_keys("11415")

    # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'location'))).send_keys('11415')
    # button_to_be_clicked = browser.find_element(By.ID, 'button-submit')
    # button_to_be_clicked.click()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(By.XPATH, "//input[@class='form-control' and @id='location']")).send_keys("11415")

    print(driver.page_source)

initalize_browser()


driver.quit

I have watched a ton of videos and read some answers on stack overflow, but I believe the syntax has changed and I haven't found too much info. I would really appreciate any help, thank you so much

CodePudding user response:

I used the https://github.com/seleniumbase/SeleniumBase Recorder & Export tool to generate a working script.

First pip install seleniumbase, then run with pytest or python:

from seleniumbase import BaseCase

class HealthSiteTest(BaseCase):
    def test_health_site(self):
        self.open("https://www.fairhealthconsumer.org/")
        self.click('button:contains("Medical and Hospital Costs")')
        self.click('label[for="radio3"] span i:nth-of-type(2)')
        self.click('label[for="radio3"] mark span')
        self.type("input#location", "11415")
        self.click('a[href="/medical/select-medical-totalcost"]')
        self.assert_text("Select the type of care", "h1")

# When run with "python" instead of "pytest"
if __name__ == "__main__":
    from pytest import main
    main([__file__])

CodePudding user response:

It looks like the element you're trying to interact with is an input element with a type attribute of "text" and a class attribute of "form-control". You can interact with an input element on a website by using its type attribute of "text" and class attribute of "form-control". To locate this element, you can use the By.XPATH locator and the following XPath expression:

//input[@type='text' and @class='form-control']
  • Related