user3471881
user3471881

Reputation: 2724

Check if element contains ANY and then clear (Selenium/Python)

I'm using Selenium to visit a website and then perform a bunch of different searches. This is the basic set up:

searches = ['search 1', 'search2']
path_to_chromedriver = 'C:\Python34\chromedriver' # change path as needed
driver = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'https://thisisthesite.com'
driver.get(url)

I try to check if element contains and clear like so, obviously not working:

for search in searches:
    inputElement = driver.find_element_by_id("q")
    inputText = EC.presence_of_element_located((By.XPATH, '//*[@id="q" and text() != ""]'))
    if inputText > 0:
        inputElement.clear()
    else:
        inputElement.send_keys(search)
        inputElement.submit()

So I want to test if inputElement contains ANY text and then clear it before performing my searches.

Upvotes: 0

Views: 738

Answers (1)

Andersson
Andersson

Reputation: 52675

To check if any text already present in input field and erase it if it so, you can use following code sample:

inputElement = driver.find_element_by_id("q")
if len(inputElement.get_attribute('value')) > 0:
    inputElement.clear()

Upvotes: 1

Related Questions