问题
I am trying to fill out this form automatically using Selenium. The form consists out of two pages, which both need to be filled. One proceeds to the second page by clicking the orange button stating 'Weiter'.
I have the following code,
# load form into chrome, directly via its url
ad_url = 'https://www.immobilienscout24.de/expose/97655130'
form_url_end = '#/basicContact/email'
url = ad_url + form_url_end
browser = webdriver.Chrome()
browser.get(url)
# code which fills out first page correctly
# switch to second page
window_before = browser.window_handles[0]
browser.find_element_by_xpath(
'//*[@id="is24-de"]/div[2]/div/div[2]/div[1]/form/div/div/div[4]/div[2]/button',
).click()
window_after = browser.window_handles[1]
browser.switch_to.window(window_after)
This codes runs fine until the last line –
browser.switch_to.window(window_after)
, which yields IndexError: list index out of range
.
I've followed this answer.
How do I command selenium to focus on the new page?
To fill out the fields in the form, I use e.g for the city field on the first page,
city = browser.find_element_by_id("contactForm-city")
city.clear()
city.send_keys('Berlin')
来源:https://stackoverflow.com/questions/45354850/focus-on-second-page-form-with-selenium