Inputting a value in a textbox using python seleniumbase

Question:

I decided to play around with seleniumbase for a project I am working on, and have been trying to figure out the syntax a bit. On the GitHub site, I found a lot of code, documentation, and examples showing off self.click() and self.type(). I am trying to get my code to enter the term "MRI" in the procedure box of this website, but it is not working, and most of the code I am trying to access does not have an ID value. I was hoping someone could go through my syntax and shed light on why this is the case.

This is my code:

from seleniumbase import BaseCase

class HealthSiteTest(BaseCase):
    def test_health_site(self):
        self.open("https://www.finestrahealth.com/")
        self.assert_element_present("Finestra")
        self.type("input#Procedure", "mri")

if __name__ == "__main__":
    from pytest import main
    main([__file__])

and this is the html for what I am trying to access:

enter image description here

Any help is very appreciated, thank you!

Answers:

In your script, you had self.assert_element_present("Finestra"), but the input for that should be a CSS Selector or XPath, not text. For a text assert, you could do self.assert_text("TEXT"), self.assert_text("TEXT", "SELECTOR") (for a substring search), or self.assert_exact_text("TEXT", "SELECTOR").

Here’s a script that’ll verify text and fill in fields on that page:

from seleniumbase import BaseCase

class RecorderTest(BaseCase):
    def test_recording(self):
        self.open("https://www.finestrahealth.com/")
        self.assert_exact_text("finestra", 'a[href="/"] span')
        self.type('input[placeholder="Procedure"]', "mri")
        self.type('input[placeholder="Zip code"]', "02142")
        self.type('input[placeholder="Insurance"]', "aetna")
        self.click('button:contains("Search")')
        self.assert_element('main p:contains("Hospitals")')
        self.sleep(2)

if __name__ == "__main__":
    from pytest import main
    main([__file__])

You can use the SeleniumBase Recorder to generate the above script from manual clicks and typing text. There’s a lot of documentation (and a video) for the Recorder on the SeleniumBase GitHub page.

If you run the above script using pytest --demo, you’ll see the browser actions as they occur, making it easier to see what the script is doing. Otherwise it may run too fast for your eyes to see what it does (but it will still run successfully at full speed, or even faster in headless mode).

Answered By: Michael Mintz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.