简体   繁体   中英

selenium: How to get page source code after clicking a button

I'm using selenium with python to test my web server. What I need is to fill text in an input-text and click a button to submit messages to my server and open a new web page.

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("http://www.example.com")
txt = driver.find_element_by_id('input-text')
txt.clear()
txt.send_keys('some messages')
btn = driver.find_element_by_id('input-search')
btn.click()
# How to print the source code of the new page here?
# I think it should be as below but I don't know how to do it:
# driver.get("new link")   <---- How to get the new link?
# print(driver.page_source.encode('utf-8'))

When I execute the python code above, everything works fine:

www.example.com is opened.
The some messages are filled in the input-text.
The button input-search is clicked.
The new web page is opened.

Now I need to print the page source code of the new web page, which is opened by clicking the button, but I don't know how to do this.

print(driver.page_source.encode('utf-8')) just gives me the source code of www.example.com , instead of the new web page.

If the new page is loaded in a new tab or window, you will need to change the context of selenium to this new window first:

driver.switch_to_window(driver.window_handles[1])

The next step is to wait until the new content is loaded and then you can get the code with:

print(driver.page_source.encode('utf-8'))

You will need to wait for the new page to load then. Depending on how the webpage is built, you might have to wait for an element to appear and then dump the contents of the page in a variable with driver.page_source. Alternatively, if you don't need to click a button to reach the page, you can just driver.get(url) and rely on it to finish after the page finishes loading.

Before you print the new website source code, you can try a delay of a few seconds.

time.sleep(2)
print(driver.page_source.encode('utf-8'))

This works for me :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM