Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

python - Switching into new window using Selenium after button click

I'm working on quite a simple webpage project and got stuck a bit. I'm using a website that after filling the form and clicking the button gives me my desired data in for of XML document opening in a new window. However, I have no clue how to access it, as I am not given the window name

browser = Browser('firefox')
browser.visit('http://desiredurl/')
form = browser.find_by_id('input')
button = browser.find_by_id('send')
form.fill(string)
button.click()

Clicking on a button triggers an ajax request (doAjaxRequest("POST", url, xml);) and opens a new window with XML document. What is the best way to access data from opened XML?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution for your question will be to induce Explicitwait with expected_conditions as number_of_windows_to_be and then switch over to the new window as follows :

parent = driver.current_window_handle
button = browser.find_by_id('send')
form.fill(string)
button.click()
WebDriverWait(driver, 10).until(
    EC.number_of_windows_to_be(2)
    )   
child = driver.window_handles[1]      
driver.switch_to_window(child) 
print ("Child Window ID is : %s" %child)
print("Child Window Title is : %s " %(driver.title)) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...