DavidK DavidK - 1 month ago 14
Python Question

Python - Selenium : Submit form without opening new tab or new window

I have :

driver = webdriver.Firefox()


I have some url :
driver.get(url)


I have one form I want to submit :

elt = driver.find_element_by_class_name('special_class')
driver.find_element_by_xpath('//button').click()


This opens a new window and I want everything to happen in one window.

I may have to submit similar things many times and then parse the output.

Is there a way to stay in the same window ? I don't want to have many windows opened.

Thanks in advance.

Answer

I am afraid there is nothing you can do if the button is opening new window after form submission. Only thing which comes to my mind is to use headless browser like PhantomJS - since you are doing web crawling you could appreciate the speed too. See this tutorial for Python.

But I actually remembered there is a workaround for this, you can set Firefox browser.link.open_newwindow to 1, this should cause every new window to be opened in current one. However I am not sure if this will work well with HTML form.

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.link.open_newwindow", 1)
browser = webdriver.Firefox(firefox_profile=fp)

See reference for this feature here.

Comments