0

I am trying to loop through filling form with Selenium Python. So far I can fill the first form and get the popup message. What I want is to get the next element in the loop but It seems I can not clear the previous filled text.

Here is my code:

import time 
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys


postnummer = [42435, 42448]
street_name =["Bredfjällsgatan", "Fjällviolen"]
street_number = [12, 5]


inputPost = '//*[@id="body"]/div[3]/div/div/div[5]/div/div[2]/div/a/form/input[1]'
inputStreet = '//*[@id="body"]/div[3]/div/div/div[5]/div/div[2]/div/a/form/input[2]'
inputNumber = '//*[@id="body"]/div[3]/div/div/div[5]/div/div[2]/div/a/form/input[3]'

submitButton = '//*[@id="body"]/div[3]/div/div/div[5]/div/div[2]/div/a/form/span[4]'
closeButton = '//*[@id="body"]/div[1]/div/div/div/div[2]/a'

#result = '//*[@id="body"]/div[1]/div/div/div/div[1]/div/p/text()[1]'


def sleep():
    time.sleep(3)

browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get("https://www.framtidensbredband.se/")

i = 0
while i < len(postnummer):
    browser.find_element_by_xpath(inputPost).send_keys(postnummer[i])
    browser.find_element_by_xpath(inputStreet).send_keys(street_name[i])
    browser.find_element_by_xpath(inputNumber).send_keys(street_number[i])
    sleep()
    browser.find_element_by_xpath(submitButton).click()
    sleep()
    #print(browser.find_element_by_css_selector(".alertbox .message").text)
    elem = browser.find_element_by_xpath("//*[@class='message']")
    print(elem.text)

    browser.find_element_by_xpath(closeButton).click()
    sleep()
    #browser.clear()
    #browser.find_element_by_xpath((inputPost).send_keys(postnummer[i])).clear()
    #browser.find_element_by_xpath((inputStreet).send_keys(street_name[i])).clear()
    #browser.find_element_by_xpath((inputNumber).send_keys(street_number[i])).clear()
    #sleep()

browser.quit()

I tried clear() but I think I am not using properly.

Scraping website: https://www.framtidensbredband.se/

Result Image Example

1 Answer 1

1

To clear the field you need to use element.clear() To iterate the loop and use next value you need to increment the counter.

Code:

i = 0
while i < len(postnummer):

    browser.find_element_by_xpath(inputPost).send_keys(postnummer[i])
    browser.find_element_by_xpath(inputStreet).send_keys(street_name[i])
    browser.find_element_by_xpath(inputNumber).send_keys(street_number[i])
    sleep()
    browser.find_element_by_xpath(submitButton).click()
    sleep()

    elem = browser.find_element_by_xpath("//*[@class='message']")
    print(elem.text)

    browser.find_element_by_xpath(closeButton).click()
    sleep()

    browser.find_element_by_xpath(inputPost).clear()
    browser.find_element_by_xpath(inputStreet).clear()
    browser.find_element_by_xpath(inputNumber).clear()
    sleep()
    i=i+1
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That works perfectly. I missed incrementing the loop and the same time clearing the values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.