Focus on second page form with Selenium
Asked Answered
I

0

2

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')
Issy answered 27/7, 2017 at 15:17 Comment(6)
I do not see multiple windows on that page, only dialogs and layers.Burkitt
Ah... I thought as the second page has a different url it was a 'new' window. How do I command Selenium to focus on the second page?Issy
Selenium will "focus" on whatever is currently in the one window.Burkitt
If that is correct, then why am I able to fill in the fields on the first page, but using similar code on the second page, I receive a NoSuchElementException? I've added the code I use to fill the fields in the question.Issy
Ok, just solved it. It needed to wait a bit: https://mcmap.net/q/245074/-selenium-webdriver-nosuchelementexceptionsIssy
I do not see any element with id=contactForm-city on the second page.Burkitt

© 2022 - 2024 — McMap. All rights reserved.