mercredi 21 juin 2017

python selenium: how to visit a web page many times on the same page

I'm using selenium with python to test my web server. Here is my test code:

i = 0
msg = 'abc'
while i < 10:
    driver = webdriver.Chrome()
    driver.get("http://www.example.com")
    txt = driver.find_element_by_id('input-text')
    txt.clear()
    txt.send_keys(msg)
    btn = driver.find_element_by_id('input-search')
    btn.click()
    driver.quit()
    i += 1

The code works well except only one thing: it executes Chrome, do the test and close it for each time of loop. Obviously it's not necessary. What I need is simply to execute Chrome only one time and do many requests. I've tried as below but it doesn't work:

i = 0
msg = 'abc'
driver = webdriver.Chrome()
while i < 10:
    driver.get("http://www.example.com")
    txt = driver.find_element_by_id('input-text')
    txt.clear()
    txt.send_keys(msg)
    btn = driver.find_element_by_id('input-search')
    btn.click()
    i += 1
driver.quit()

I think it's because in my test, there are two things:
1) fill abc in input-text;
2) click a button, submit the abc and open a new web page.

It seems that when the second thing is done, the loop stop immediately.

Aucun commentaire:

Enregistrer un commentaire